Created
December 4, 2014 13:37
-
-
Save bendisposto/b8ca57cf1bc8dfcec10f to your computer and use it in GitHub Desktop.
Code aus der Vorlesung vom 4.12.2014
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 repl.greeter.friendly-hello | |
| (:require [clojure.string :as str])) | |
| (defn msg! [& args] | |
| (println "Hello" (str/join " and " args))) |
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 repl.core | |
| (:refer-clojure :exclude [replace]) | |
| (:require [clojure.test :as t]) | |
| (:use [clojure.string :only (replace join)] | |
| [clojure.repl :rename {dir ls}]) | |
| (:import (java.util Date Timer Random) | |
| java.io.File | |
| (javax.swing JFrame JPanel))) | |
| (defn- internal [] -1) | |
| (comment | |
| ;; Wiederholung | |
| ;; ------------------------------------------------------------------------------------ | |
| (def log-file (agent [])) | |
| (defn debug [& words] | |
| (send log-file | |
| (fn [x] | |
| (Thread/sleep 3000) | |
| (conj x (apply str (interpose " " words)))))) | |
| (debug "foo" "bar") | |
| log-file | |
| (doc debug) | |
| (defn debug | |
| "Append some words to the log file" | |
| [& words] | |
| (send log-file | |
| (fn [x] | |
| (Thread/sleep 3000) | |
| (conj x (apply str (interpose " " words)))))) | |
| (doc debug) | |
| (add-watch log-file :log (fn [_ _ _ _] (println "Log changed." (System/currentTimeMillis)))) | |
| (time | |
| (do | |
| (debug "Eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll") | |
| (debug "Noch" "eine" "Ausgabe"))) | |
| (time (do (debug "foo" "bar" ) (await log-file) @log-file)) | |
| ;; future & promise | |
| (def x (future (do (Thread/sleep 5000) 12))) | |
| x | |
| (def x (future (do (Thread/sleep 5000)))) | |
| (deref x 100 :timeout) | |
| x | |
| (def x (promise)) | |
| x | |
| (deliver x 10) | |
| @x | |
| ;; destructuring | |
| (let [[_ a & r :as l] [1 2 3]] (println a r l)) | |
| (let [point {:x -210 :y 7100} | |
| {:keys [x y z] :or {x 0 y 0 z 0} :as p} point] (println x y z p)) | |
| (let [{x :x y :y} {:x 5 :y 10}] [y x]) | |
| ;; Laden von Modulen | |
| ;; 1) Java Klassen | |
| Console | |
| java.io.Console | |
| (import java.io.Console) | |
| Console | |
| (type Console) | |
| ;; 2) Clojure Namespaces | |
| (repl.greeter.friendly-hello/msg! "John" "Michael") | |
| (require 'repl.greeter.friendly-hello) | |
| ;; require = load | |
| ;; Datei $CLASSPATH/repl/greeter/friendly_hello.clj | |
| ;; Inhalt: | |
| ;; (ns repl.greeter.friendly-hello | |
| ;; (:require [clojure.string :as str])) | |
| ;; (defn msg! [& args] | |
| ;; (println "Hello" (str/join " and " args))) | |
| (repl.greeter.friendly-hello/msg! "John" "Michael") | |
| (msg! "John" "Michael") | |
| ;; Alias | |
| (require '[repl.greeter.friendly-hello :as hello]) | |
| (hello/msg! "John" "Jens") | |
| zipper ;; error | |
| clojure.zip/zipper | |
| (require 'clojure.zip) | |
| zipper | |
| clojure.zip/zipper | |
| (refer 'clojure.zip :except '[next replace]) | |
| zipper | |
| ;; use = require + refer | |
| ;; ------------------------------------------------------------------------------------ | |
| ;; ------------------------------------------------------------------------------------ | |
| ;; Namespace Macro (s. oben) | |
| ;; ------------------------------------------------------------------------------------ | |
| ;; Vars definieren | |
| (def v 4) | |
| ;; was geanu ist #'repl.core/v ? | |
| (type #'repl.core/v) | |
| (var v) | |
| (doc var) | |
| ;; def assoziiert das Symbol v mit einem Var | |
| ;; Das Var ist ein Container für einen Wert | |
| ;; Die assoziation findet im Namespace statt | |
| ;; Namespaces sind maps (so in etwa jedenfalls) | |
| ;; Evaluation von Symbolen | |
| ;; ------------------------------------------------------------------------------------ | |
| 1 | |
| :a | |
| [:a 1] | |
| {:a 1} | |
| (+ 1 2) | |
| ding | |
| (defn foo [] 42) | |
| foo | |
| (foo) | |
| (var foo) | |
| #'foo | |
| (#'foo) | |
| @#'foo | |
| (@#'foo) | |
| (def bar1 foo) | |
| bar1 | |
| (bar1) | |
| #'bar1 | |
| #'foo | |
| (def bar2 #'foo) | |
| bar2 | |
| (def bar3 #'bar2) | |
| bar3 | |
| @bar3 | |
| @@bar3 | |
| (eval bar3) | |
| (type (eval bar3)) | |
| (bar3) | |
| ;; Evaluationsreihenfolge | |
| ;; ------------------------------------------------------------------------------------ | |
| ;; 1) qualified symbols | |
| (def java.io.Writer 101) | |
| repl.core/java.io.Writer | |
| java.io.Writer | |
| ;; 2) Java-Class | |
| (let [java.io.Writer 12] java.io.Writer) ;; error | |
| ;; 3) locals | |
| (def foo 10) | |
| (let [foo 11] foo) | |
| (let [foo 11] repl.core/foo) ;; Regel 1 | |
| ;; 4) Symbols | |
| foo | |
| ;; 5) Error | |
| sdfjks | |
| ;; Namespaces | |
| ;; ------------------------------------------------------------------------------------ | |
| (use 'repl.greeter.friendly-hello) | |
| ;; repl | |
| (def m (ns-map 'repl.greeter.friendly-hello)) | |
| (keys m) | |
| ;; repl!!! | |
| (first m) | |
| (map type (first m)) | |
| (def f (get m (symbol "msg!"))) | |
| (f "jens" "michael") | |
| (apropos "ns-") | |
| (def n 'repl.core) | |
| (ns-publics n) | |
| (keys (ns-publics n)) | |
| (keys (ns-interns n)) | |
| (clojure.set/difference | |
| (set (keys (ns-interns n))) | |
| (set (keys (ns-publics n)))) | |
| (map type (first (ns-interns n))) | |
| (keys (ns-imports n)) | |
| (map type (first (ns-imports n))) | |
| (ns-aliases n) | |
| (first (ns-refers n)) | |
| t/do-report | |
| (ns-unalias n 't) | |
| t/do-report | |
| (msg! "you") | |
| (def msg! 17) ;; error | |
| (ns-unmap n 'msg!) | |
| (def msg! (constantly 17)) | |
| (msg! "you") | |
| (def msg! repl.greeter.friendly-hello/msg!) | |
| (msg! "you") | |
| (def msg! (constantly 17)) ;; Warum geht das jetzt? | |
| ;; Clojure - Java Interop | |
| ;; ------------------------------------------------------------------------------------ | |
| (File. ".") ;; new File(".") | |
| (macroexpand-1 '(File. ".")) | |
| (doc new) | |
| Math/PI ;; Math.PI | |
| (def r (Random.)) | |
| (.nextInt r) ;; r.nextInt() | |
| (.nextInt r 100) ;; r.nextInt(100) | |
| (.. r nextInt toString hashCode) ;; r.nextInt().toString().hashCode() | |
| #_(defn t [f] (.start (Thread. f))) ;; new Thread(f).start() | |
| ;; vgl. ->, ->> | |
| (-> [1 2 3 4] (conj ,,, 5) reverse) | |
| (reverse (conj [1 2 3 4] 5)) | |
| (->> [1 2 3 4] (map inc ,,,) (filter even? ,,,) (reduce + ,,,)) | |
| (reduce + (filter even? (map inc [1 2 3 4]))) | |
| ;; --- | |
| (doto | |
| (JFrame. "Tralala") | |
| (.setSize 800 600) | |
| (.setVisible true)) | |
| ;; j = new JFrame("Tralala"); | |
| ;; j.setSize(800,600); | |
| ;; j.setVisible(true); | |
| ;; Warum mögen Clojure Programmierer doto nicht besonders? | |
| ;; ------------------------------------------------------------------------------------ | |
| ;; Java -> Clojure | |
| ;; idiomatischer Java code (apache.commons): | |
| ;; public static boolean isBlank(final CharSequence cs) { | |
| ;; int strLen; | |
| ;; if (cs == null || (strLen = cs.length()) == 0) { | |
| ;; return true; | |
| ;; } | |
| ;; for (int i = 0; i < strLen; i++) { | |
| ;; if (Character.isWhitespace(cs.charAt(i)) == false) { | |
| ;; return false; | |
| ;; } | |
| ;; } | |
| ;; return true; | |
| ;; } | |
| ;; Schritt 1: Weg mit den Types | |
| ;; public isBlank(cs) { | |
| ;; if (cs == null || (strLen = cs.length()) == 0) { | |
| ;; return true; | |
| ;; } | |
| ;; for (int i = 0; i < strLen; i++) { | |
| ;; if (Character.isWhitespace(cs.charAt(i)) == false) { | |
| ;; return false; | |
| ;; } | |
| ;; } | |
| ;; return true; | |
| ;; } | |
| ;; Schritt 2: HOF | |
| ;; public isBlank(cs) { | |
| ;; if (cs == null || (strLen = cs.length()) == 0) { | |
| ;; return true; | |
| ;; } | |
| ;; return every (c in cs) { | |
| ;; Character.isWhitespace(c) | |
| ;; } | |
| ;; } | |
| ;; Schritt 3: Corner Cases eliminieren | |
| ;; public isBlank(cs) { | |
| ;; return every (c in cs) { | |
| ;; Character.isWhitespace(c) | |
| ;; } | |
| ;; } | |
| ;; Schritt 4: Clojure Syntax | |
| (defn blank? [cs] | |
| (every? #(Character/isWhitespace %) cs)) | |
| (blank? " x ") | |
| (blank? " ") | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment