<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 問題 Problem 有人需要在限制重重的地方運行部分你的代碼,或者你的語言無法簡潔地表達其他領域的問題。 Someone else needs to run parts of your code in a controlled fashion. Alternately, your language of choice cannot express the problem domain in a concise fashion. ## 方案 Solution 使用解釋器模式創建一個領域語言,翻譯成特殊的代碼。 Use the Interpreter pattern to create a domain-specific language that you translate into specific code. 例如,有用戶想在你的程序中執行數學計算。你可以讓他們把代碼交給_eval_運行,但是他們有可能會運行各種各樣的代碼。更好的方案,你可以提供一個小型的“棧計算器”語言,單獨解析,這樣可以只允許運行數學操作,同時還可以反饋更有用的錯誤信息。 Assume, for example, that the user wants to perform math inside of your application. You could let them forward code to?_eval_?but that would let them run arbitrary code. Instead, you can provide a miniature “stack calculator” language that you parse separately in order to only run mathematical operations while reporting more useful error messages. ~~~ class StackCalculator parseString: (string) -> @stack = [ ] for token in string.split /\s+/ @parseToken token if @stack.length > 1 throw "Not enough operators: numbers left over" else @stack[0] parseToken: (token, lastNumber) -> if isNaN parseFloat(token) # Assume that anything other than a number is an operator @parseOperator token else @stack.push parseFloat(token) parseOperator: (operator) -> if @stack.length < 2 throw "Can't operate on a stack without at least 2 items" right = @stack.pop() left = @stack.pop() result = switch operator when "+" then left + right when "-" then left - right when "*" then left * right when "/" if right is 0 throw "Can't divide by 0" else left / right else throw "Unrecognized operator: #{operator}" @stack.push result calc = new StackCalculator calc.parseString "5 5 +" # => { result: 10 } calc.parseString "4.0 5.5 +" # => { result: 9.5 } calc.parseString "5 5 + 5 5 + *" # => { result: 100 } try calc.parseString "5 0 /" catch error error # => "Can't divide by 0" try calc.parseString "5 -" catch error error # => "Can't operate on a stack without at least 2 items" try calc.parseString "5 5 5 -" catch error error # => "Not enough operators: numbers left over" try calc.parseString "5 5 5 foo" catch error error # => "Unrecognized operator: foo" ~~~ ## 討論 Discussion 如果不自己寫解釋器, 你可以結合目前有的CoffeeScrtipt解釋器,以某種方式,這種方式使用它平常的語法創建算法的更加自然的(因此更易于理解)表達方式。 As an alternative to writing our own interpreter, you can co-op the existing CoffeeScript interpreter in a such a way that its normal syntax makes for more natural (and therefore more comprehensible) expressions of your algorithm. ~~~ class Sandwich constructor: (@customer, @bread='white', @toppings=[], @toasted=false)-> white = (sw) -> sw.bread = 'white' sw wheat = (sw) -> sw.bread = 'wheat' sw turkey = (sw) -> sw.toppings.push 'turkey' sw ham = (sw) -> sw.toppings.push 'ham' sw swiss = (sw) -> sw.toppings.push 'swiss' sw mayo = (sw) -> sw.toppings.push 'mayo' sw toasted = (sw) -> sw.toasted = true sw sandwich = (customer) -> new Sandwich customer to = (customer) -> customer send = (sw) -> toastedState = sw.toasted and 'a toasted' or 'an untoasted' toppingState = '' if sw.toppings.length > 0 if sw.toppings.length > 1 toppingState = " with #{sw.toppings[0..sw.toppings.length-2].join ', '} and #{sw.toppings[sw.toppings.length-1]}" else toppingState = " with #{sw.toppings[0]}" "#{sw.customer} requested #{toastedState}, #{sw.bread} bread sandwich#{toppingState}" send sandwich to 'Charlie' # => "Charlie requested an untoasted, white bread sandwich" send turkey sandwich to 'Judy' # => "Judy requested an untoasted, white bread sandwich with turkey" send toasted ham turkey sandwich to 'Rachel' # => "Rachel requested a toasted, white bread sandwich with turkey and ham" send toasted turkey ham swiss sandwich to 'Matt' # => "Matt requested a toasted, white bread sandwich with swiss, ham and turkey" ~~~ 上面這個例子,允許連續調用多個函數,因為每個函數都會返回修改后的對象,這樣外圍的函數就可以輪流地修改該對象。借用一個一個非常小的_to_,該例子給于構造過程了一個更加自然的語法,并且如果使用正確的話,讀起來就像愛那個一個自然的句子。這樣的話,你的CoffeeScript技術和你在使用的語言技術都能幫助你找到代碼的問題。 This example allows for layers of functions by how it returns the modified object so that outer functions can modify it in turn. By borrowing a very and the particle?_to_, the example lends natural grammar to the construction and ends up reading like an actual sentence when used correctly. This way, both your CoffeeScript skills and your existing language skills can help catch code problems.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看