Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.
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
| import java.util.List; | |
| import java.util.ArrayList; | |
| /* | |
| * Lexical analyzer for Scheme-like minilanguage: | |
| * (define (foo x) (bar (baz x))) | |
| */ | |
| public class Lexer { | |
| public static enum Type { | |
| // This Scheme-like language has three token types: |
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组合子是Lambda演算的一部分,也是函数式编程的理论基础。 | |
| 它是一种方法/技巧,在没有赋值语句的前提下定义递归的匿名函数。 | |
| 即仅仅通过Lambda表达式这个最基本的“原子”实现循环/迭代。 | |
| 颇有道生一、一生二、二生三、三生万物的感觉。 | |
| 虽然Y组合子在理论上很优美,但在实际开发中并不会真的用到。 | |
| 想要了解Y组合子是什么,请参见维基百科:http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator | |
| 或者知乎上的回答:http://www.zhihu.com/question/20115649 |
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
| // Compositional evaluator as visitor | |
| import java.math.BigInteger; | |
| // Syntax | |
| interface Exp { | |
| <X> X accept(Visitor<X> v); | |
| } |
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 fizzbuzz [n] | |
| (let [all-nums (range 0 n) | |
| folder (fn [fb-str p-num fb-coll] | |
| (mapcat (fn [x] (cons fb-str (rest x))) | |
| (partition-all p-num fb-coll))) | |
| fizz (folder "fizz" 3 all-nums) | |
| buzz (folder "buzz" 5 fizz) | |
| fizzbuzz (folder "fizzbuzz" 15 buzz)] | |
| fizzbuzz)) |
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
| ;;fp语言之所以有抽象能力或者说非fp语言是告诉机器如何一步一步解决问题 而fp语言是向机器描述问题 然后让机器给我们答案其中一个语言原语就是高阶函数 也就是函数可以被当做参数和返回值 还有闭包的存在 函数不再是对象的附属 而是独立的个体 甚至LC中 只有单参函数这一个基本元素却能做到图灵完备 正所谓大道至简 | |
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 CHEATSHEET | |
| ;; | |
| ;; * :require makes functions available with a namespace prefix | |
| ;; and optionally can refer functions to the current ns. | |
| ;; | |
| ;; * :import refers Java classes to the current namespace. | |
| ;; | |
| ;; * :refer-clojure affects availability of built-in (clojure.core) | |
| ;; functions. |
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
| (let [expression (read-string "(+ 1 2 3 4 5)")] | |
| (cons (read-string "*") | |
| (rest expression))) | |
| ;;=> (* 1 2 3 4 5) | |
| (eval *1) | |
| ;;=> 120 | |
| (let [expression '(+ 1 2 3 4 5)] | |
| (cons '* (rest expression))) |
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
| db.tabclicktime.update( | |
| {tabid: 8}, | |
| {$set: {title: "电视剧"}}, | |
| {multi: true} | |
| ); | |
| //-1 -> desc 1 -> asc | |
| db.tabclicktime.find({tabid: 8}).sort({timestamp: -1}) |
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
| val a : Option[String] = Some("333") | |
| //a: Option[String] = Some(333) | |
| a.filter(_.length > 1).map(_.length) | |
| //Option[Int] = Some(3) | |
| a.filter(_.length > 6).map(_.length) | |
| //Option[Int] = None | |
| //filter and map function is just for Option type it's just a monad | |
| //def filter(p: A => Boolean): Option[A] | |
| //def map[B](f: A => B): Option[B] |