This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Chain do | |
| def counter(pid) do | |
| receive do | |
| n -> | |
| send pid, n+1 | |
| end | |
| end | |
| def create_processes(n) do | |
| last = Enum.reduce 1..n, self(), fn (_, pid) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn unfold | |
| "Creates a lazy sequence based on start accumulator and step functon. | |
| The step function transform the state as below: | |
| step(value) -> [current-value, next-accumulator] | |
| For example: | |
| (unfold 5 (fn [n] | |
| (when-not (zero? n) | |
| [n (dec n)]))) | |
| " |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| y = fn (f) -> | |
| (fn (x) -> | |
| f.(x.(x)) | |
| end).( | |
| fn (x) -> | |
| f.(x.(x)) | |
| end) | |
| end | |
| z = fn (f) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MySigils do | |
| def sigil_i(string, []), do: String.to_integer(string) | |
| def sigil_i(string, [?n]), do: -String.to_integer(string) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defprotocol Blank do | |
| @doc "Returns true if data is considered blank/empty" | |
| @fallback_to_any true | |
| def blank?(data) | |
| end | |
| defimpl Blank, for: Any do | |
| def blank?(_), do: false | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyUnless do | |
| defmacro unless(clause, then_clause, else_clause \\ nil) do | |
| quote do | |
| if !unquote(clause) do | |
| unquote(then_clause) | |
| else | |
| unquote(else_clause) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule CounterSupervisor do | |
| use Supervisor | |
| def start_link(v \\ 0) do | |
| Supervisor.start_link(__MODULE__, v) | |
| end | |
| @counter_name Counter | |
| #supervisor callback | |
| def init(v) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Counter do | |
| use GenServer | |
| def start_link(init_value \\ 0, opts \\ []) do | |
| GenServer.start_link(__MODULE__, init_value, opts) | |
| end | |
| ## client apis | |
| def get(c) do | |
| GenServer.call(c, {:get}) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns cljcomputionbook.tm | |
| (:require [clojure.string :as cs])) | |
| ;;磁带 | |
| (defrecord Tape [left middle right blank] | |
| Object | |
| (toString [tape] | |
| (pr-str tape))) | |
| (defmethod print-method Tape [tape writer] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns fizz-buzz | |
| "《计算的本质》第 6 章使用 clojure 基于 lambda 演算实现 FizzBuzz 程序。") | |
| (def zero (fn [p] (fn [x] x))) | |
| (def one (fn [p] (fn [x] (p x)))) | |
| (def two (fn [p] (fn [x] (p (p x))))) | |
| (def three (fn [p] (fn [x] (p (p (p x)))))) | |
| (def four (fn [p] (fn [x] (p (p (p (p x))))))) | |
| (def five (fn [p] (fn [x] (p (p (p (p (p x)))))))) |