Quellcode durchsuchen

Initial commit. Let's start playing (in public).

Hello, Languages! I hope to meet many of you.

If you, dear reader, happen to know a language that I have neglected, I
invite you to kindly point me towards it. I love learning new things and
I have quite a lot of place in my heart for languages that teach me new
things.

And now, go forth and explore things new to you.

(And have a nice day, of course.)
Lucas Stadler vor 13 Jahren
Commit
1a60afa0e2
5 geänderte Dateien mit 266 neuen und 0 gelöschten Zeilen
  1. 16 0
      README.md
  2. 18 0
      clj/README.md
  3. 180 0
      clj/hello-clojure.clj
  4. 13 0
      clj/joy/README.md
  5. 39 0
      clj/joy/sql.clj

+ 16 - 0
README.md

1
# language playground
2
3
Hi there! Welcome to my little language playground. I use this
4
repository to help myself learning several programming languages, try
5
out ideas while also sharing the code with others.
6
7
So far I have written too much code that has never seen the light of
8
day, because it's in my *other* playground repository, which contains
9
all kinds of secrets and is thus not public. But I think it might be
10
valuable to share the experience of learning and playing with
11
programming languages. So here I am, enjoy yourself!
12
13
## Languages
14
15
* current:
16
    - [clojure](http://clojure.org): A modern Lisp on the JVM.

+ 18 - 0
clj/README.md

1
# Learning (aka. playing with) Clojure
2
3
- first heard about it early 2012 (I think)
4
- dipped my feet in a few times since then
5
- came across (i.e. read or saw things they did) a few interesting
6
  people using/writing Clojure in interesting way (fogus, Rich Hickey,
7
  Chris Granger)
8
- now reading 'The Joy of Clojure' (and enjoying it)
9
10
## Tools
11
12
- mostly LightTable
13
- previously Vim with the VimClojure plugin, but not right now
14
  (LightTable 'feels' more interactive to me and what I miss most from
15
  Vim (keybindings) will be in LT soonish, I also think that something
16
  like LT has more potential to be extensible and has a much saner
17
  extension language)
18
- the `clj` repl

+ 180 - 0
clj/hello-clojure.clj

1
(ns hello-clojure
2
  (:require [clojure.repl :as repl])
3
  #_(:use zetta.core)
4
  #_(:require [zetta.combinators :as c])
5
  #_(:require [zetta.parser.seq :as p]))
6
7
(defn human-list
8
  [l]
9
  (let [len (count l)]
10
    (cond (= len 0)
11
          ""
12
          (= len 1)
13
          (str (first l))
14
          :else
15
          (apply str
16
                 (concat (butlast (interleave (map str (butlast l)) (repeat ", ")))
17
                         [(str " and " (last l))])))))
18
19
(human-list [])
20
(human-list [1])
21
(human-list [1 2])
22
(human-list [1 2 3])
23
(println (human-list (take 11 (repeat "love"))))
24
25
(defn same-start [s ss]
26
  (= (apply str (take (count ss) s)) ss))
27
28
(same-start "sorted-map" "foo")
29
(same-start "sorted-map" "sorted-")
30
31
(defn filter-ns
32
  [filter-fun namespace]
33
  (let [syms (map str (keys (ns-publics namespace)))]
34
    (filter filter-fun syms)))
35
36
(defn complete
37
  [to-complete namespace]
38
  (filter-ns #(.startsWith % to-complete) 'clojure.core))
39
40
(defn complete'
41
  [to-complete namespace]
42
  (filter-ns #(same-start % to-complete) namespace))
43
44
(complete' "st" 'clojure.core)
45
(print (filter-ns #(.endsWith % "?") 'clojure.core))
46
(= (complete "take" 'clojure.core) (complete' "take" 'clojure.core))
47
48
(def letsample
49
  '(let [x 10]
50
     (+ x 3)))
51
52
(defn replace-vars
53
  [[fun bindings body]]
54
  '(fun bindings
55
        body))
56
57
(replace-vars letsample)
58
59
(repl/doc replace)
60
(replace { 'x 10 } '(+ x 13 [10 x 13]))
61
62
(for [x '[1 2 3 4 y]] x) ; i need a for that retains the original seqs
63
64
(map inc '(1 2 3))
65
; already 'good'
66
(map inc [1 2 3])
67
(into [] (map inc [1 2 3]))
68
(map inc { 1 1 2 2 3 3 })
69
; ???
70
(map inc #{1 2 3 4 5})
71
(into #{} (map inc #{1 2 3 4 5}))
72
73
(defn map-retain
74
  [f coll]
75
  (let [target (cond (vector? coll)
76
                     []
77
                     (set? coll)
78
                     #{}
79
                     :else
80
                     nil)]
81
    (if target
82
    	(into target (map f coll))
83
      (map f coll))))
84
85
(map-retain inc '(1 2 3))
86
(map-retain inc [1 2 3])
87
(map-retain inc #{1 2 3 4 5})
88
89
(defn map-retain-rec
90
  [f coll]
91
  (map-retain #(if (coll? %)
92
                 (map-retain-rec f %)
93
                 (f %))
94
              coll))
95
96
(map-retain-rec inc '(1 2 3 [4 5 6] #{1 2}))
97
(map-retain-rec str '(let [x 10] (+ x 11)))
98
(map-retain-rec #(or ({'x 10 'y 42 'z -3} %) %) '(let [x 10] (* y (+ x 1))))
99
(map-retain-rec #(or ({'coll [1 2 3]} %) %) '(defn map-retain
100
                                               [f coll]
101
                                               (let [target (cond (vector? coll)
102
                                                                  []
103
                                                                  (set? coll)
104
                                                                  #{}
105
                                                                  :else
106
                                                                  nil)])))
107
108
(def all-vars
109
  (fn all-vars
110
    [vars body]
111
    (reduce #(cond (symbol? %2)
112
                   (conj %1 %2)
113
                   (coll? %2)
114
                   (all-vars %1 %2)
115
                   :else
116
                   %1)
117
            vars body)))
118
119
(def letsample '(let [[x y z] [1 2 3]
120
                     {a :a b :b c :c} {:a 4 :b 5 :c 6}]))
121
(sort (all-vars #{} letsample))
122
123
(reduce #(assoc %1 `'~%2 %2) {} (all-vars #{} letsample))
124
(repl/doc assoc)
125
126
(defmacro bind
127
  [bindings]
128
  (let [bindings (eval bindings)] ; macro arguments are usually not evaluated?
129
    															; is there a shorter/more ideomatic way?
130
    `(let ~bindings
131
       ~(reduce #(assoc %1 `'~%2 %2)
132
                {}
133
                (all-vars #{} bindings)))))
134
135
(bind (second letsample))
136
(bind '[[x y z] [1 2 3] {a :a b :b c :c} {:a 4 :b 5 :c 6}])
137
138
(repl/doc binding)
139
(defn read-file
140
  [filename]
141
  (with-open [file (java.io.PushbackReader.
142
                    (clojure.java.io/reader filename))]
143
    (binding [*read-eval* false]
144
      (doall
145
        (take-while
146
          #(not= % nil)
147
          (repeatedly #(read file false nil)))))))
148
149
(def hello-forms
150
  (let [forms (read-file "/home/lu/k/lp/clj/hello-clojure.clj")]
151
  { :number-of-forms (count forms)
152
    :forms forms }))
153
hello-forms
154
155
(bind
156
	(let [human-list-defn (nth (hello-forms :forms) 1)]
157
    (println (first (drop-while #(not (vector? %)) human-list-defn)))
158
    (println (first (drop-while #(not (list? %)) human-list-defn)))
159
  	(into '[l [1 2 3]] (nth (nth human-list-defn 3) 1)))) ; bind in here yields an InstantiationException
160
161
(defn apropos+
162
  "Search a namespace for a symbol whose documentation contains `string`"
163
  ([string]
164
   (apropos+ string 'clojure.core))
165
  ([string namespace]
166
   (let [symbols (keys (ns-publics namespace))
167
         docs (map #(-> `#'~% eval meta :doc) symbols)]
168
     (filter #(and (not= nil %) (.contains % string)) docs))))
169
170
(apropos+ "containing" 'clojure.core)
171
(repl/doc apropos+)
172
173
(defn p
174
  ([x]
175
   (p x println))
176
  ([x print-func]
177
   (print-func x)
178
   x))
179
180
(filter true? (map #(p (= 42 %)) [1 2 3 4 5]))

+ 13 - 0
clj/joy/README.md

1
# Notes for The Joy of Clojure
2
3
Notes and Code regarding 'The Joy of Clojure'. The code is either
4
directly copied or directly inspired by the book. I'll add a note in
5
each file when the former is the case.
6
7
Regarding the copied code, I *think* that it's ok to use it, but I don't
8
know for sure. However, it *is* [availlable online][src], so I am
9
probably ok doing this. However, if you are Michael Fogus or Chris
10
Houser and don't like me copying the code here, please tell me and I'll
11
take it away.
12
13
[src]: https://github.com/joyofclojure/book-source

+ 39 - 0
clj/joy/sql.clj

1
(ns joy-sql
2
  "A tiny DSL for SQL.
3
  
4
  Note: This code is taken verbatim from The Joy of Clojure."
5
  (:use [clojure.string :as str :only []]))
6
7
(defn expand-expr [expr]
8
  (if (coll? expr)
9
    (if (= (first expr) `unquote)
10
      "?"
11
      (let [[op & args] expr]
12
        (str "(" (str/join (str " " op " ")
13
                           (map expand-expr args))
14
             ")")))
15
    expr))
16
17
(declare expand-clause)
18
19
(def clause-map
20
  {'SELECT (fn [fields & clauses]
21
             (apply str "SELECT " (str/join ", " fields)
22
                    (map expand-clause clauses)))
23
   'FROM (fn [table & joins]
24
            (apply str " FROM " table
25
                   (map expand-clause joins)))
26
   'LEFT-JOIN (fn [table on expr]
27
                (str " LEFT JOIN " table
28
                     " ON " (expand-expr expr)))
29
   'WHERE (fn [expr]
30
            (str " WHERE " (expand-expr expr)))})
31
32
(defn expand-clause [[op & args]]
33
  (apply (clause-map op) args))
34
35
(defmacro SELECT [& args]
36
  [(expand-clause (cons 'SELECT args))
37
   (vec (for [n (tree-seq coll? seq args)
38
              :when (and (coll? n) (= (first n) `unquote))]
39
          (second n)))])