定义和使用EL函数

 

package mypack; 

import java.util.*; 

 

public class Tool { 

    public static int add(String x, String y) { 

        int a = 0; 

        int b = 0; 

        try { 

            a = Integer.parseInt(x); 

            b = Integer.parseInt(y); 

        }catch(Exception e) {}    

    

        return a + b; 

    } 

 

    public static String convert(String s,String encode1,String encode2){ 

        try{ 

            return new String(s.getBytes(encode1),encode2); 

        }catch(Exception e){return null;} 

     } 

} 

 

<%@ page contentType="text/html; charset=GB2312" %> 

<%@ taglib prefix="mm" uri="/mytaglib" %> 

<html> 

<head> 

<title>functions</title> 

</head> 

 

<body> 

 

<h3>Add Numbers</h3> 

<p> 

<form action="sum.jsp" method="get"> 

     user= <input type="text" name="user"    

                value="${mm:convert(param.user,'ISO-8859-1','GB2312')}"> 

     <br> 

    

     x = <input type="text" name="x" value="${param.x}"> 

     <br> 

     y = <input type="text" name="y" value="${param.y}"> 

     <input type="submit" value="Add Numbers"> 

</form> 

 

<p> 

the sum is: ${mm:add(param.x,param.y)} 

 

</body> 

</html>

mytaglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?> 

 

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" 

        version="2.0"> 

        <tlib-version>1.1</tlib-version> 

        <short-name>mytaglib</short-name> 

        <uri>/mytaglib</uri> 

         

 

    <function> 

        <description>add x and y</description> 

        <name>add</name> 

        <function-class>mypack.Tool </function-class> 

        <function-signature> 

             int add(java.lang.String,java.lang.String) 

        </function-signature> 

    </function> 

 

    <function> 

        <description>convert encoding</description> 

        <name>convert</name> 

        <function-class>mypack.Tool </function-class> 

        <function-signature> 

             java.lang.String convert(java.lang.String,java.lang.String,java.lang.String) 

        </function-signature> 

    </function> 

 

</taglib> 

web.xml

    

    <taglib> 

            <taglib-uri>/mytaglib</taglib-uri> 

            <taglib-location>/WEB-INF/mytaglib.tld</taglib-location> 

     </taglib>

 

从 <http://ndebyq.blog.51cto.com/274018/148005> 插入

 

 

 

源文档 <http://hi.baidu.com/cliff77/blog/item/2ccfaf381bd28721b9998fcb.html>

 

<%@ taglib prefix="my" uri="http://jakarta.apache.org/tomcat/jsp2-example-taglib"%>
<html>
<head>
    <title>JSP 2.0 Expression Language - Functions</title>
</head>
<body>
    <h1>JSP 2.0 Expression Language - Functions</h1>
    <hr>
    An upgrade from the JSTL expression language, the JSP 2.0 EL also
    allows for simple function invocation. Functions are defined
    by tag libraries and are implemented by a Java programmer as 
    static methods.

    <blockquote>
      <u><b>Change Parameter</b></u>
      <form action="functions.jsp" method="GET">
foo = <input type="text" name="foo" >
          <input type="submit">
      </form>
      <br>
      <code>
        <table border="1">
          <thead>
    <td><b>EL Expression</b></td>
    <td><b>Result</b></td>
</thead>
<tr>
    <td>\${param["foo"]}</td>
    <td>${fn:escapeXml(param["foo"])} </td>
</tr>
<tr>
    <td>\${my:reverse(param["foo"])}</td>
    <td>${my:reverse(fn:escapeXml(param["foo"]))} </td>
</tr>
<tr>
    <td>\${my:reverse(my:reverse(param["foo"]))}</td>
    <td>${my:reverse(my:reverse(fn:escapeXml(param["foo"])))} </td>
</tr>
<tr>
    <td>\${my:countVowels(param["foo"])}</td>
    <td>${my:countVowels(fn:escapeXml(param["foo"]))} </td>
</tr>
</table>
      </code>
    </blockquote>
</body>
</html>

Eclim 安装

 

Installing / Upgrading

Requirements

Before beginning the installation, first confirm that you have met the following requirements.

  • Vim 7.1.x
    Minimum Vim Settings: In order for eclim to function properly, there is a minimum set of vim options that must be enabled in your vimrc file (:h vimrc).
    • set nocompatible
      Execute :h ‘compatible’ for more info. You can confirm that compatibliity is turned off by executing the following:
      :echo &compatible

      Which should output ‘0’.
    • filetype plugin on
      Execute :h filetype-plugin-on for more info. You can confirm that file type plugins are enabled by executing the following:
      :filetype

      Which should output ‘filetype detection:ON plugin:ON indent:ON’, showing at least ‘ON’ for ‘detection’ and ‘plugin’.

Eclim Graphical Installer

Step 1: Download and run the installer.

Note

If you have eclipse running, please close it prior to starting the installation procedure.

  • First download the installer: eclim_<version>.jar
  • Next run the installer:
    $ java -jar eclim_<version>.jar

    Windows and OSX users should be able to simply double click on the jar file to start the installer.
    Note
    If you are behind a proxy, you may need to run the installer like so (be sure to take a look at the related
    faq as well):
    $ java -jar eclim_<version>.jar -Dhttp.proxyHost=my.proxy -Dhttp.proxyPort=8080

    If your proxy requires authentication, you’ll need to supply the -Dhttp.proxyUser and -Dhttp.proxyPassword properties as well.
    You can also try the following which may be able to use your system proxy settings:
    $ java -jar eclim_<version>.jar -Djava.net.useSystemProxies=true

After the installer starts up, simply follow the steps in the wizard to install the application.

Note

In some rare cases you might encounter one of the following errors:

  1. Any exception which denotes usage of gcj.
    java.lang.NullPointerException
      at org.pietschy.wizard.HTMLPane.updateEditorColor(Unknown Source)
      at org.pietschy.wizard.HTMLPane.setEditorKit(Unknown Source)
      at javax.swing.JEditorPane.getEditorKit(libgcj.so.90)
      ...
    Gcj (GNU Compile for Java), is not currently supported. If you receive any error which references libgcj, then gcj is your current default jvm. So, you’ll need to install a sun jvm to resolve the installation error.
  2. java.lang.IncompatibleClassChangeError
      at org.formic.ant.logger.Log4jLogger.printMessage(Log4jLogger.java:51)
      ...
    This is most likely caused by an incompatible version of log4j installed in your jave ext.dirs. To combat this you can run the installer like so:
    $ java -jar eclim_<version>.jar -Djava.ext.dirs

Step 2: Testing the installation

To test eclim you first need to start the eclim daemon. How you start the daemon will depend on how you intend to use eclim.

Note

More info on running the eclim daemon can be found in the eclimd docs.

If you plan on using eclim along with the eclipse gui, then:

  • start eclipse with the -clean option
    $ eclipse -clean
    Note
    You should only need to start eclipse with the -clean option the first time after installing or upgrading eclim.
  • open the eclimd view
    Window -> Show View -> Other -> Eclim -> eclimd

If you plan on using eclim without the eclipse gui, then:

  • start the eclimd server.
    • Linux / Mac / BSD (and other unix based systems): To start eclimd from linux, simply execute the eclimd script found in your eclipse root directory:
      $ $ECLIPSE_HOME/eclimd
    • Windows: The easiest way to start eclimd in windows is to double click on the eclimd.bat file found in your eclipse root directory: %ECLIPSE_HOME%/eclimd.bat

Once you have the eclim daemon (headed or headless) running, you can then test eclim:

  • open a vim window and issue the command, :PingEclim. The result of executing this command should be the eclim and eclipse version echoed to the bottom of your Vim window. If however, you receive unable to connect to eclimd - connect: Connection refused, or something similar, then your eclimd server is not running or something is preventing eclim from connecting to it. If you receive this or any other errors you can start by first examining the eclimd output to see if it gives any info as to what went wrong. If at this point you are unsure how to proceed you can view the troubleshooting guide or feel free to post your issue on the eclim user mailing list.
    Example of a successful ping:
    Example of a failed ping:
  • Regardless of the ping result, you can also verify your vim settings using the command :EclimValidate. This will check various settings and options and report any problems. If all is ok you will receive the following message:
    Result: OK, required settings are valid.

What’s Next

Now that you have eclim installed, the next step is to familiarize yourself with at least the core set of commands that eclim provides, all of which are found at the index of the eclim documentation.

After doing that you can then proceed to getting started guide.

Upgrading

The upgrading procedure is the same as the installation procedure but please be aware that the installer will remove the previous version of eclim prior to installing the new one. The installer will delete all the files in the eclim eclipse plugins and the files eclim adds to your .vim or vimfiles directory. So if you made any alterations to any of these files, be sure to back them up prior to upgrading.

Building from source

If you would like to use the bleeding edge development version of eclim or you would like to contribute code, then you can checkout and build eclim from source. Instructions on doing so can be found in the developers guide.

Unattended (automated) install

As of eclim 1.5.6 the eclim installer supports the ability to run an automated install without launching the installer gui.

Warning

When using this method no validation is performed to ensure that you have the required third party eclipse plugin dependencies necessary for the eclim features you’ve chosen to install. It is the responsibility of the user, or the script which launches the installer, to validate the dependencies prior to installation. This installation method is primarily provided for those wishing to package eclim for inclusion in a package management system.

Here is an example of installing eclim with only java and ant support using this method:

$ java -jar eclim_<version>.jar install \

  -Declipse.home=/opt/eclipse \

  -Dvim.files=$HOME/.vim \

  -DfeatureList.ant=true \

  -DfeatureList.jdt=true

As you can see by the example, the values normally obtained from the user by the graphical installer are supplied using java system properties. This method of installation has only two required properties that must be set and various optional properties to enable features, etc.

Required:

  • eclipse.home - The absolute path to the eclipse installation.
  • vim.files - The absolute path to the vim files directory.

Optional:

  • eclipse.local - When installing for a single user, some eclipse installations have a user local location where eclipse plugins are installed. This property can be set to that location.
  • eclim.gvim - The location of the gvim executable to be set as the default for embedding gvim inside of eclipse.

Optional Feature Properties: All of the following properties must have the value ‘true’ to enable the feature. All other values, or no value at all will result in the exclusion of that feature. Also, some features require that other features be enabled, as noted below:

  • featureList.ant (requires jdt)
  • featureList.cdt
  • featureList.dltk
  • featureList.dltkruby (requires dltk)
  • featureList.jdt
  • featureList.maven
  • featureList.pdt (requires wst and dltk)
  • featureList.python
  • featureList.wst

 

源文档 <http://eclim.org/guides/install.html>

新东方李老师的734条高频词组笔记

1.abide by(=be faithful to ; obey)忠于;遵守。  
2. be absent from…. 缺席,不在  
3. absence or mind(=being absent-minded) 心不在焉  
4. absorb(=take up the attention of)吸引…的注意力(被动语态)be absorbed in 全神贯注于…   近义:be engrossed ; be lost in ; be rapt in ;be concentrated on  ; be focused on ; be centered on  
5. (be) abundant in(be rich in; be well supplied with) 富于,富有     6. access(to) (不可数名词) 能接近,进入,了解  
7. by accident(=by chance, accidentally)偶然地,意外. Without accident(=safely) 安全地,  
8. of one’s own accord(=without being asked; willingly; freely)自愿地 ,主 动地  
9. in accord with 与…一致 . out of one’s accord with 同….不一致  
10. with one accord (=with everybody agreeing)一致地  
11. in accordance with (=in agreement with) 依照,根据  
12. on one’s own account  1) 为了某人的缘故, 为了某人自己的利益  2) (=at one’s own risk) 自行负责  3) (=by oneself)依靠自己  on account 赊账; on account of 因为; on no account不论什么原因也不;of …account 有…..重要性.  
13. take…into account(=consider)把...考虑进去  
14. give sb. an account of 说明, 解释 (理由)  
15. account for (=give an explanation or reason for) 解释, 说明.  
16. on account of (=because of) 由于,因为.  
17. on no account(=in no case, for no reason)绝不要,无论如何不要(放句首时句 子要倒装)  
18. accuse…of…(=charge…with; blame sb. for sth. ; blame sth. on sb. ; complain about) 指控,控告  
19. be accustomed to (=be in the habit of, be used to)习惯于.  
20. be acquainted with(=to have knowledge of) 了解; (=to have met socially  ) 熟悉  
21. act on 奉行,按照…行动; act as 扮演; act for 代理  
22. adapt oneself to(=adjust oneself to) 使自己适应于  
23. adapt…(for) (=make sth. Suitable for a new need) 改编, 改写(以适应新的需要)  
24. in addition (=besides) 此外, 又, 加之  
25. in addition to(=as well as, besides, other than)除…外  
26. adhere to (=abide by, conform to, comply with, cling to, insist on, pe rsist in, observe, opinion, belief ) 粘附; 坚持, 遵循  
27. adjacent(=next to, close to) 毗邻的, 临近的  
28. adjust..(to) (=change slightly)调节; 适应;  
29. admit of (=be capable of, leave room for) …的可能,留有…的余地.  
30. in advance (before in time) 预告, 事先.  
31. to advantage 有利的,使优点更加突出地.  
32. have an advantage over 胜过.  have the advantage of 由于…处于有利条件  have the advantage of sb.知道某人所不知道的事  
33. take advantage of (=make the best of, utilize, make use of, profit from, harness)利用.  
34. agree with 赞同(某人意见) agree to 同意  
35. in agreement (with) 同意, 一致  
36. ahead of 在…之前, 超过…;……………. ahead of time 提前.  
37. in the air 1)不肯定, 不具体. 2)在谣传中.  
38. above all (=especially, most important of all) 尤其是, 最重要的.  
39. in all (=counting everyone or everything, altogether) 总共, 总计  
40. after all 毕竟,到底; (not) at all 一点也不;  all at once(=suddenly)突然; once and for all 只此一次; above all 最重要的; first of all 首先; all in all 大体上说; be all in 累极了; all but 几乎.  
41. allow for (=take into consideration, take into account) 考虑到, 估计到  .  
42. amount to (=to be equal to) 总计, 等于.  
43. answer for (undertake responsibility for, be liable for, take charge for) 对…负责.  
44. answer to (=conform to) 适合,符合.  
45. be anxious about 为…焦急不安; 或anxious for  
46. apologize to sb. for sth. 为…向…道歉  
47. appeal to sb. for sth. 为某事向某人呼吁. appeal to sb. 对某人有吸引力  
48. apply to sb. for sth. 为…向…申请 ; apply for申请; apply to 适用.  
49. apply to 与…有关;适用  
50. approve of (=consent to, be in favor of, favor, agree to, consider good, right) 赞成, approve vt. 批准  
51. arise from(=be caused by) 由…引起.  
52. arrange for sb.sth. to do sth. 安排…做…  
53. arrive on 到达; arrive at 到达某地(小地方);得出,作出; arrive in 到达某地(大地方);  
54. be ashamed of (=feel shame, guilt or sorrow because of sth. done) 以…  为羞耻  
55. assure sb. of sth. (=try to cause to believe or trust in sth.) 向…保证, 使…确信.  
56. attach(to) (=to fix, fasten; join) 缚, 系 ,结  
57. make an attempt at doing sth. (to do sth.) 试图做…  
58. attend to (=give one’s attention, care and thought)注意,照顾;attend on(upon)(=wait upon, serve, look after) 侍候,照料  
59. attitude to toward …对…的态度.看法  
60. attribute…to…(=to believe sth. to be the result of…)把..归因于.., 认为..是..的结果  
61. on the average (=on average, on an average) 平均  
62. (be) aware of (=be conscious of , having knowledge or consciousness)意识到,知道.  
63. at the back of (=behind) 在…后面  
64. in the back of 在…后部(里面); on the back of 在…后部(外面); be on one’s back(=be ill in bed) 卧病不起.  
65. at one’s back(=supporting or favoring sb.) 支持,维护; have sb. at one ’s back 有…支持, 有…作后台  
66. turn one’s back on sb. (=turn away from sb. in an impolite way) 不理睬(某人),背弃,抛弃  
67. behind one’s back 背着某人(说坏话)  
68. be based on  upon 基于  
69. on the basis of 根据…, 在…基础上  
70. beat…at 在…运动项目上打赢  
71. begin with 以…开始. to begin with (=first of all) 首先, 第一(经常用于开始语)  
72. on behalf of (=as the representative of) 以…名义  
73. believe in(=have faith or trust in; consider sth.sb. to be true) 相信,依赖,信仰.  
74. benefit (from) 受益,得到好处.  
75. for the benefit of 为了…的利益(好处)  
76. for the better 好转  
77. get the better of (=defeat sb.) 打败, 胜过.  
78. by birth 在出生上,论出身,按血统 at birth 在出生时; give birth to 出生  
79. blame sb. for sth. 因…责备某人 . blame sth. on sb. 把…推在某人身上  
80. in blossom开花(指树木) be in blossom开花(强调状态) come into blossom开花(强调动作)  
81. on board 到船上, 在船上, 上火车或飞机  
82. boast of (or about) 吹嘘  
83. out of breath 喘不过气来  
84. in brief(=in as few words as possible)简言之  
85. in bulk 成批地,不散装的  
86. take the floor 起立发言  
87. on business 出差办事.  
88. be busy with sth.于某事 。 be busy doing sth. 忙于做某事  
89. last but one 倒数第二.  
90. but for (=without) 要不是. 表示假设  
91. buy sth. for…money 用多少钱买  
92. be capable of 能够, 有能力  be capable of being +过去分词是能够被…的  
93. in any case(=for love or money, at any rate, at any price, at any cost  , whatever happens; anyhow)无论如何  
94. in case (=for fear that) 万一;  
95. in case of (=in the event of)如果发生…万一  in the case of 至于…, 就…而言  
96. in no case在任何情况下都不(放句首倒装句)  
97. be cautious of 谨防  
98. center one’s attention on(=focus one’s attention on) 把某人的注意力集中在…上  
99. be certain of (=be sure of) 有把握, 一定.  
100. for certain of (=for sure )肯定地,有把握地  
101. by chance(=accidentally, by accident)偶然  
102. for a change换换环境(花样等)  
103. charge sb. with …控告某人犯有…  
104. in charge of (=responsible for) 负责(某事)  in the charge of …由…管  
105. take charge of (=to be or become responsible for)负责管理(照顾)  
106. charge…for 因…索取(费用) , charge sb. with sth. 控告某人犯有…  
107. round the clock(=all day and all night, usually without stopping) 昼夜不停地  
108. comment on 评论  
109. commit oneself to 使自己承担… commit sb. to prison把某人送进监狱; commit one’s idea to writing 把某人的想法写下来; commit a matter to a committee  把某事交给委员会讨论  
110. in common (和…)有共同之处,共用.  be common to sb. 是与某人所共有的  
111. keep company with (=be friendly and go out together) 和…要好.  
112. compare…with … 把…与…比较  
113. compare…to… 把…比作…  
114. by comparison 比较起来  
115. in comparison with (=in contrast to) 和…比起来  
116. compensate for (=give sth. to make up for) 补偿, 赔偿,弥补 compensate  sb. for sth. 赔偿,弥补  
117. complain of (or about)抱怨;诉苦;控告;complain about 抱怨某人或事情; complain to sb. about sth. (or sb.) 向某人抱怨…; complain (抱怨); complement (补充); compliment (恭维)  
118. comply with (=act in accordance with a demand, order, rule etc.) 遵守, 依从  
119. conceive of (think of, imagine, consider) 想象,设想  
120. concentrate on (or upon) 集中,专心  
121. be concerned with (=about) 与…有关  
122. concern oneself about  with 关心  
123. in conclusion(=as the last thing)最后一点; at the conclusion of 当…结束时;  
124. condemn sb. to 判决  
125. on condition that (=if)以…为条件, 假如. in that = because因为; now that = since既然  for all that = although 尽管  
126. in  out of condition (=thoroughly healthy or fit  not fit) 健康状况好不好 . in good (bad) condition处于良好(坏)状态  
127. confess(to)(=admit a fault, crime, or sth. wrong)承认, 供认; confess to a crime 承认罪行.  
128. confide in (=to talk freely to sb. about one’s secret) 对…讲真心话, 依赖  
129. in confidence 推心置腹地; with confidence 满怀信心地; have confidence in 对…有信心  
130. confidence in sb.  sth. 对…的信赖  
131. be confident of 有信心; confidential 机密的  
132. confine…to… 把…限制在某范围内  
133. confirm sb. in 使某人更坚定(信念等)  
134. conform to (=be in agreement with, comply with) 符合,遵照,遵守;1)obey 服从; 2) observe; 3)comply with照…办; 4)keep to遵循; 5)abide by服从;6)stick to按..做  
135. be confronted with(=be brought face to face with) 面对, 面临  
136. congratulate sb. on 祝贺  
137. in connection with(=with regard to)关于,  
138. be conscious of(=be aware of)觉察,知道  
139. consent to(=give agreement to permission)同意  
140. in consequence (=as a result) 结果  
141. in consequence of (=as a consequence of)由于…的结果  

1.         142. under consideration 在考虑中  
143. in consideration of (=in return for, on account of, because of )由于  
144. on no consideration(in no case)无论如何也不  
145.take…into consideration (=take account of, take…into account)考虑到, 把 …考虑进去  
146. considerate (=thoughtful of the needs) 体贴的, 考虑他人需要的, considerable相当大的,值得考虑的  
147. consist of(=be composed of)由…组成的. consist in主要在于. consist with符合,与…一致  
148. be consistent with(=be in agreement with)与…一致. be consistent in一贯的  ,  
149. consult sb. on about sth. 向…征求…方面的意见, 就…向…请教  
150. to one’s heart’s content尽情地,痛痛快快  
151. be content with(=be satisfied with) 满足于be content to do sth. 愿意做某事  
152. contrary to (=in opposition to) 与…相反  
153. on the contrary 相反  
154. contrast…with 把…与…相对(对照)  
155. in contrast towith 和…形成对比 by contrast 对比之下  
156. contribute to 有助于  
157. under control (被)控制住 out of control无法控制  
158. at one’s convenience(=where and when it suits one) 在方便的时间或地点 .be convenient to  for 对…方便  
159. convince sb. of (=cause sb. to believe or feel certain; to persuade sb.)  使某人确信,try to persuade sb. to do sth.劝说某人做…  
160. cope with(=deal with, try to find a solution to)应付, 处理  
161. in the corner(of)在角落里;on(at) the comer of a street在街道拐弯处;round  the comer拐过弯; be in a tight corner陷入困境  
162. correspond (with) ( =exchange letters regularly) 通信  
163. correspond to 相当于 . correspond with 符合,一致  
164. at all costs不惜任何代价 . at the cost of 以…为代价  
165. a matter of course 理所当然的事  
166. as a matter of course 当然地, 自然地  
167. in (during) the course 在…过程中  
168. in due course (=without too much delay) 没经过太久, 到一定时候  
169. on credit赊购; with credit以优异成绩; to one’s credit使某人感到光荣; do sb. credit 使…感到光荣  
170.be critical of 爱挑毛病的,批评的  
171. cure sb. of+某种疾病 治好某人的疾病  
172.a danger to对…的危险; be in danger(of)处于…危险中; be out of danger脱离危险  
173. to date(=so far, until now) 到目前为止  
174. out of date过时的;up to date新式的,时兴的; date back to可追溯到; date from从某时期开始(有)  
175. deal with (=concern) 论及  
176. be in debt to sb. 欠…的债  

177. on the decline 在衰退中, 在减少中  in decline 下降; on the increase 在增加  
178. to one’s delight 令某人感到高兴  to one’s regret 遗憾; sorrow悲痛; relief 安心; distress 苦恼; shame羞愧; surprise 惊奇; astonishment 惊奇;  
179. delight in(=take great pleasure in doing sth.)喜欢, 取乐  
180. take (a) delight in 喜欢干…, 以…为乐  
181. demand sth. of sb. 向某人要求(非物质的)东西. demand sth. from sb. 向某人要求(物质的)东西  
182.in demand有需求;on demand受到要求时  
183. be dependent on 依靠  
184. deprive sb. of sth. 剥夺某人某物  
185. derive…from(=obtain…from)从…取得,由…来的.derive from(=come from)起源于  
186. despair of (=lose all hope of) 绝望  
187. in despair 绝望  
188. despite (=in spite of) 不管, 尽管  
189. in detail 详细地  
190. deviate from 偏离, 不按…办  
191. on a diet 吃某种特殊饮食, 节食  
192. differ from…in 与…的区别在于…  
193. in difficulties…有困难,处境困难,  
194. discharge sb. (from)…for (=dismiss sb. from a job for) 因…解雇, 开除  
195. fall back (=retreat, turn back) 撤退;  in disorder 慌乱地, 狼狈不堪  
196. on display(=being shown publicly)陈列  
197. dispose of (=get rid of ,throw away)处理掉  
198. beyond dispute不容争议的,无可争议  
199. in dispute 在争议中  
200. in the distance 在远处. make out 辩认出  
201. (be) distinct from ( = be different from) 与…截然不同  
202. distinguish between (=make or recognize differences) 辨别  
203. distinguish…from 把…与…区别开  
204. do away with(=get rid of; abolish; discard eliminate) 除去,废除,取消; do away with (=kill) 杀掉, 镇压  
205. have…to do with 与…有关系  
206. without doubt (=undoubtedly)无可置疑地  
207. in doubt(=in a condition of uncertainty)对…表示疑惑  
208. be due to 是由于  
209. come off duty 下班  
210. go on duty 上班  
211. be on duty 值班, 值日, 在上班时  
212. be in duty bound to (do) (=be required by one’s job or esp. by conscience) 有义务(做)  
213. be eager for 想得到, 盼望  
214. by ear (=play music from memory without having seen it printed) 凭记忆,不看乐谱  
215. have an ear for (=have keen recognition of sounds esp. in music and language)对..有鉴赏力  
216. a word in one’s ear 私房话, 秘密话  
217. on earth 究竟, 到底, 全然  
218. with ease ( = easily) 容易, 不费力  
219. at (one’s) ease ( = without worry or nervousness) 自在,不拘束  
220. put sb. at his  her ease (=free sb. from worry or nervousness)使某人感到无拘束  
221. economize on (=save sth. instead of being wasteful) 节省  
222. have an effect on 对…有影响  
223. be in effect (=be in operation) 有效  
224. go into effect 生效. ( 近 come into effect; take effect; be brought into effect)  
225. in effect (=in fact, really) 实际上  
226. give effect to (=carry out) 实行,使…生效  
227. to no effect 不起作用,没有取得任何效果  
228. (be) of no effect (=useless) 无效  
229. to the effect that 大意是…,主要内容是…  
230. to that effect 是那个意思的…  
231. emerge from ( =appear, become known ) 出现, 暴露(问题. 意见等)  
232. place(or put, lay) an emphasis on 强调, 把重点放在…上  
233. encourage sb. in 鼓励; encourage sb. in hisher work 鼓励某人工作; encourage sb. in hisher idleness 怂恿某人游手好闲  
234. encourage sb. in …with sth. 用…鼓励某人做某事  
235. on end (=continuously) 连续地  

236. (be) at an end (=finished) 结束了

尾递归与Continuation

 

 

关于递归操作,相信大家都已经不陌生。简单地说,一个函数直接或间接地调用自身,是为直接或间接递归。例如,我们可以使用递归来计算一个单向链表的长度:

 

public class Node
{
    public Node(int value, Node next)
    {
        this.Value = value;
        this.Next = next;
    }

public int Value { get; private set; }

public Node Next { get; private set; }
}

编写一个递归的GetLength方法:

public static int GetLengthRecursively(Node head)
{
    if (head == null) return 0;
    return GetLengthRecursively(head.Next) + 1;
}

在调用时,GetLengthRecursively方法会不断调用自身,直至满足递归出口。对递归有些了解的朋友一定猜得到,如果单项链表十 分长,那么上面这个方法就可能会遇到栈溢出,也就是抛出StackOverflowException。这是由于每个线程在执行代码时,都会分配一定尺寸 的栈空间(Windows系统中为1M),每次方法调用时都会在栈里储存一定信息(如参数、局部变量、返回地址等等),这些信息再少也会占用一定空间,成 千上万个此类空间累积起来,自然就超过线程的栈空间了。不过这个问题并非无解,我们只需把递归改成如下形式即可(在这篇文章里我们不考虑非递归的解法):

public static int GetLengthTailRecursively(Node head, int acc)
{
    if (head == null) return acc;
    return GetLengthTailRecursively(head.Next, acc + 1);
}

GetLengthTailRecursively方法多了一个acc参数,acc的为accumulator(累加器)的缩写,它的功能是在 递归调用时“积累”之前调用的结果,并将其传入下一次递归调用中——这就是GetLengthTailRecursively方法与 GetLengthRecursively方法相比在递归方式上最大的区别:GetLengthRecursive方法在递归调用后还需要进行一次 “+1”,而GetLengthTailRecursively的递归调用属于方法的最后一个操作。这就是所谓的“尾递归”。与普通递归相 比,由于 递归的调用处于方法的最后,因此方法之前所积累下的各种状态对于递归调用结果已经没有任何意义,因此完全可以把本次方法中留在堆栈中的 数据完全清除,把空间让给最后的递归调用。这样的优化1便使得递归不会在调用堆栈上产生堆积,意味着即时是“无限”递归也不会让堆 栈溢出。这便是尾递归的优势。

有些朋友可能已经想到了,尾递归的本质,其实是将递归方法中的需要的“所有状态”通过方法的参数传入下一次调用中。对于 GetLengthTailRecursively方法,我们在调用时需要给出acc参数的初始值:

GetLengthTailRecursively(head, 0)

为了进一步熟悉尾递归的使用方式,我们再用著名的“菲波纳锲”数列作为一个例子。传统的递归方式如下:

public static int FibonacciRecursively(int n)
{
    if (n < 2) return n;
    return FibonacciRecursively(n - 1) + FibonacciRecursively(n - 2);
}

而改造成尾递归,我们则需要提供两个累加器:

public static int FibonacciTailRecursively(int n, int acc1, int acc2)
{
    if (n == 0) return acc1;
    return FibonacciTailRecursively(n - 1, acc2, acc1 + acc2);
}

于是在调用时,需要提供两个累加器的初始值:

FibonacciTailRecursively(10, 0, 1)

尾 递归与Continuation

Continuation即为“完成某件事情”之后“还需要做的事情”。例如,在.NET中标准的APM调用方式,便是由BeginXXX方法和EndXXX方法构成,这其实便是 一种Continuation:在完成了BeginXXX方法之后,还需要调用EndXXX方法。而这种做法,也可以体现在尾递归 造中。例如以下为阶乘方法的传统递归定义:

public static int FactorialRecursively(int n)
{
    if (n == 0) return 1;
    return FactorialRecursively(n - 1) * n;
}

显然,这不是一个尾递归的方式,当然我们轻易将其转换为之前提到的尾递归调用方式。不过我们现在把它这样“理解”: 每次计算n的阶乘时,其实是“先获取n – 1的阶乘”之后再“与n相乘并返回”,于是我们的FactorialRecursively方法可以改造成:

public static int FactorialRecursively(int n)
{
    return FactorialContinuation(n - 1, r => n * r);
}

// 6. FactorialContinuation(n, x => x)
public static int FactorialContinuation(int n, Func<int, int> continuation)
{
    ...
}

FactorialContinuation方法的含义是“计算n的阶乘,并将结果传入continuation方法,并返回其调用结果”。于 是,很容易得出,FactorialContinuation方法自身便是一个递归调用:

public static int FactorialContinuation(int n, Func<int, int> continuation)
{
    return FactorialContinuation(n - 1,
        r => continuation(n * r));
}

FactorialContinuation方法的实现可以这样表述:“计算n的阶乘,并将结果传入continuation方法并返回”,也就是“计算n – 1的阶乘,并将结果与n相乘,再调用continuation方法”。为了实现“并将结果与n相乘,再调用continuation方法”这个逻辑,代码 又构造了一个匿名方法,再次传入FactorialContinuation方法。当然,我们还需要为它补充递归的出口条件:

public static int FactorialContinuation(int n, Func<int, int> continuation)
{
    if (n == 0) return continuation(1);
    return FactorialContinuation(n - 1,
        r => continuation(n * r));
}

很明显,FactorialContinuation实现了尾递归。如果要计算n的阶乘,我们需要如下调用 FactorialContinuation方法,表示“计算10的阶乘,并将结果直接返回”:

FactorialContinuation(10, x => x)

再加深一下印象,大家是否能够理解以下计算“菲波纳锲”数列第n项值的写法?

public static int FibonacciContinuation(int n, Func<int, int> continuation)
{
    if (n < 2) return continuation(n);
    return FibonacciContinuation(n - 1,
        r1 => FibonacciContinuation(n - 2,
            r2 => continuation(r1 + r2)));
}

在函数式编程中,此类调用方式便形成了“Continuation Passing Style(CPS)”。由于C#的Lambda表达式能够轻松构成一个匿名方法,我们也可以在C#中实现这样的调用方式。您 可能会想——汗,何必搞得这么复杂,计算阶乘和“菲波纳锲”数列不是一下子就能转换成尾递归形式的吗?不过,您试试看以下的例子呢?

对二叉树进行先序遍历(pre-order traversal)是典型的递归操作,假设有如下TreeNode类:

public class TreeNode
{
    public TreeNode(int value, TreeNode left, TreeNode right)
    {
        this.Value = value;
        this.Left = left;
        this.Right = right;
    }

public int Value { get; private set; }

public TreeNode Left { get; private set; }

public TreeNode Right { get; private set; }
}

于是我们来传统的先序遍历一下:

public static void PreOrderTraversal(TreeNode root)
{
    if (root == null) return;

Console.WriteLine(root.Value);
    PreOrderTraversal(root.Left);
    PreOrderTraversal(root.Right);
}

您能用“普通”的方式将它转换为尾递归调用吗?这里先后调用了两次PreOrderTraversal,这意味着必然有一 次调用没法放在末尾。这时候便要利用到Continuation了:

public static void PreOrderTraversal(TreeNode root, Action<TreeNode> continuation)
{
    if (root == null)
    {
        continuation(null);
        return;
    }

Console.WriteLine(root.Value);

PreOrderTraversal(root.Left,
        left => PreOrderTraversal(root.Right,
            right => continuation(right)));
}

我们现在把每次递归调用都作为代码的最后一次操作,把接下来的操作使用Continuation包装起来,这样就实现了尾递归避免了堆栈数据的堆积。可见,虽然使用Continuation是一个略有些“诡异”的使用方式,但是在某些时候它也是必不可少的使用技巧。

Continuation的改进

看看刚才的先序遍历实现,您有没有发现一个有些奇怪的地方?

PreOrderTraversal(root.Left,
    left => PreOrderTraversal(root.Right,
        right => continuation(right)));

关于最后一步,我们构造了一个匿名函数作为第二次PreOrderTraversal调用的Continuation,但是其内部直接调用了 continuation参数——那么我们为什么不直接把它交给第二次调用呢?如下:

PreOrderTraversal(root.Left,
    left => PreOrderTraversal(root.Right, continuation));

我们使用Continuation实现了尾递归,其实是把原本应该分配在栈上的信息丢到了托管堆上。每个匿名方法其实都是托管堆上 的对象,虽然说这种生存周期短的对象不会对内存资源方面造成多大问题,但是尽可能减少此类对象,对于性能肯定是有帮助的。这里再举一个更为明显的例子,求 二叉树的大小(Size):

public static int GetSize(TreeNode root, Func<int, int> continuation)
{
    if (root == null) return continuation(0);
    return GetSize(root.Left,
        leftSize => GetSize(root.Right,
            rightSize => continuation(leftSize + rightSize + 1)));
}

GetSize方法使用了Continuation,它的理解方法是“获取root的大小,再将结果传入continuation,并返回其调 用结果”。我们可以将其进行改写,减少Continuation方法的构造次数:

public static int GetSize2(TreeNode root, int acc, Func<int, int> continuation)
{
    if (root == null) return continuation(acc);
    return GetSize2(root.Left, acc,
        accLeftSize => GetSize2(root.Right, accLeftSize + 1, continuation));
}

GetSize2方法多了一个累加器参数,同时它的理解方式也有了变化:“将root的大小累加到acc上,再将结果传入 continuation,并返回其调用结果”。也就是说GetSize2返回的其实是一个累加值,而并非是root参数的实际尺寸。当然,我们在调用时 GetSize2时,只需将累加器置零便可:

GetSize2(root, 0, x => x)

不知您清楚了吗?

结束

在命令式编程中,我们解决一些问题往往可以使用循环来代替递归,这样便不会因为数据规模造成堆栈溢出。但是在函数式编程中,要实现“循环”的唯 一方法便是“递归”,因此尾递归CPS对于函数式编程的意义非常重大。了解尾递归,对于编程思维也有很大帮助,因此大家不妨多 加思考和练习,让这样的方式为自己所用。

注1:事实上,在C#中,即使您实现了尾递归,编译器(包括C#编译器及JIT)也不会进行优化,也就是说还是无法避免 StackOverflowException

 

源文档 <http://blog.201314.info/2010/05/24/%E5%B0%BE%E9%80%92%E5%BD%92%E4%B8%8Econtinuation.html>

一些重要的算法

 

一些重要的算法

酷壳 - CoolShell.cn 作者:陈皓

有超过 100 人喜欢此条目

下面是一些比较重要的算法,原文列了32个,但我觉得有很多是数论里的,和计算机的不相干,所以没有选取。下面的这些,有的我们经常在用,有的基本不用。有的很常见,有的很偏。不过了解 一下也是好事。也欢迎你留下你觉得有意义的算法。(注:本篇文章并非翻译,其中的算法描述大部份摘自Wikipedia,因为维基百科描述的很专业了)

  1. A*搜寻算法
    俗称A星算法。这是一种在图形平面上,有多个节点的路径,求出最低通过成本的算法。常用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上。该算法像Dijkstra算法一样,可以找到一条最短路径;也像BFS一样,进行启发式的搜索。
  2. Beam Search
    束搜索(beam search) 方法是解决优化问题的一种启发式方法,它是在分枝定界方法基础上发展起来的,它使用启发式方法估计k 个最好的路径,仅从这k 个路径出发向下搜索,即每一层只有满意的结点会被保留,其它的结点则被永久抛弃,从而比分枝定界法能大大节省运行时间。束搜索于20 世纪70 年代中期首先被应用于人工智能领域,1976 Lowerre 在其称为HARPY的语音识别系统中第一次使用了束搜索方法,他的目标是并行地搜索几个潜在的最优决策路径以减少回溯,并快速地获得一个解。
  3. 二分取中查找算法
    一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;如果某一特定元素大于或 者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。这种搜索算法每一次比较都使搜索范围缩小一半。
  4. Branch and bound
    分支定界 (branch and bound) 算法是一种在问题的解空间树上搜索问题的解的方法。但与回溯算法不同,分支定界算法采用广度优先或最小耗费优先的方法搜索解空间树,并且,在分支定界算法中,每一个活结点只有一次机会成为扩展结点。
  5. 数据压缩
    数据压缩是通过减少计算机中所存储数据或者通信传播中数据的冗余度,达到增大数据密度,最终使数据的存储空间减少的技术。数据压缩在文件存储和分布式系统领域有着十分广泛的应用。数据压缩也代表着尺寸媒介容量的增大和网络带宽的扩展。
  6. Diffie–Hellman密钥协商
    Diffie–Hellman key exchange,简称“D–H” 是一种安全协议。它可以让双方在完全没有对方任何预先信息的条件下通过不安全信道建立起一个密钥。这个密钥可以在后续的通讯中作为对称密钥来加密通讯内容。
  1. Dijkstra’s 算法
    迪科斯彻算法(Dijkstra)是由荷兰计算机科学家艾兹格·迪科斯彻(Edsger Wybe Dijkstra)发明的。算法解决的是有向图中单个源点到其他顶点的最短路径问题。举例来说,如果图中的顶点表示城市,而边上的权重表示著城市间开车行经的距离,迪科斯彻算法可以用来找到两个城市之间的最短路径。
  1. 动态规划
    动态规划是一种在数学和计算机科学中使用的,用于求解包含重叠子问题的最优化问题的方法。其基本思想是,将原问题分解为相似的子问题,在求解的过程中通过子问题的解求出原问题的解。动态规划的思想是多种算法的基础,被广泛应用于计算机科学和工程领域。比较著名的应用实例有:求解最短路径问题,背包问题项目管理网络流优化等。这里也有一篇文章说得比较详细。
  1. 欧几里得算法
    在数学中,辗转相除法,又称欧几里得算法,是求最大公约数的算法。辗转相除法首次出现于欧几里得的《几何原本》(第VII卷,命题i和ii)中,而在中国则可以追溯至东汉出现的《九章算术》。
  1. 最大期望(EM)算法
    在统计计算中,最大期望(EM)算法是在概率probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Latent Variable)。最大期望经常用在机器学习计算机视觉数据聚类Data Clustering领域。最大期望算法经过两个步骤交替进行计算,第一步是计算期望(E),利用对隐藏变量的现有估计值,计算其最大似然估计值;第二步是最大化(M),最大 化在 E 步上求得的最大似然值来计算参数的值。M 步上找到的参数估计值被用于下一个 E 步计算中,这个过程不断交替进行。
  1. 快速傅里叶变换 (FFT)
    快速傅里叶变换(Fast Fourier Transform,FFT),是离散傅里叶变换的快速算法,也可用于计算离散傅里叶变换的逆变换。快速傅里叶变换有广泛的应用,如数字信号处理、计算大整数乘法、求解偏微分方程等等。本条目只描述各种快速算法,对于离散傅里叶变换的性质和应用,请参见离散傅里叶变换
  1. 哈希函数
    Hash Function是一种从任何一种数据中创建小的数字“指纹”的方法。该函数将数据打乱混合,重新创建一个叫做散列值的指纹。散列值通常用来代表一个短的 随机字母和数字组成的字符串。好的散列函数在输入域中很少出现散列冲突。在散列表和数据处理中,不抑制冲突来区别数据,会使得数据库记录更难找到。
  1. 堆排序
    Heapsort
    是指利用堆积树)这种数据结构所设计的一种排序算法。堆积树是一个近似完全二叉树的结构,并同时满足堆积属性:即子结点的键值或索引总是小于(或者大于)它的父结点。
  1. 归并排序
    Merge sort
    是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
  1. RANSAC 算法
    RANSAC 是”RANdom SAmple Consensus”的缩写。该算法是用于从一组观测数据中估计数学模型参数的迭代方法,由Fischler and Bolles1981 提出,它是一种非确定性算法,因为它只能以一定的概率得到合理的结果,随着迭代次数的增加,这种概率是增加的。 该算法的基本假设是观测数据集中存在”inliers”(那些对模型参数估计起到支持作用的点)和”outliers”(不符合模型的点),并且这组观测 数据受到噪声影响。RANSAC 假设给定一组”inliers”数据就能够得到最优的符合这组点的模型。
  1. RSA加密演算法
    这是一个公钥加密算法,也是世界上第一个适合用来做签名的算法。今天的RSA已经专利失效,其被广泛地用于电子商务加密,大家都相信,只要密钥足够长,这个算法就会是安全的
  1. 并查集Union-find
    并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
  1. Viterbi algorithm
    寻找最可能的隐藏状态序列(Finding most probable sequence of hidden states)

附录

 

 

源文档 <http://www.google.com/reader/view/>