Created
August 1, 2010 06:04
-
-
Save qertoip/503008 to your computer and use it in GitHub Desktop.
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
; Definicja przestrzeni nazw z importem funkcji Clojura i klas Javy | |
(ns qertoip | |
(:use | |
[clojure.contrib.io :only [to-byte-array]] | |
[clojure.contrib.java-utils :only [as-file]]) | |
(:import | |
[java.io File FileOutputStream])) | |
; W poniższy sposób można zaimportować *.clj lub *.class wymienione z nazwy, | |
; znajdujące się w CLASSPATH. Nie można zaimportować całego pakietu. | |
(use '[lib-name :only (var-names+)]) |
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
; odpowiednik w ruby: | |
; compositions.select{|c| c.name == "Requiem" }.map( &:composer ) | |
(for [c compositions :when (= "Requiem" (:name c))] (:composer c)) |
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
; Jak wołać metody obiektu? | |
(.metodaObiektu obiekt) |
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
; Jak utworzyć obiekt Javy? | |
(new Thread | |
(fn [] ()) | |
) |
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
; W clojure nie ma klas znanych z programowania obiektowego. | |
; Najbliższym odpowiednikiem jest struktura z polami, coś jak struct w C. | |
(defstruct person :first-name :last-name) | |
(struct person "Piotr" "Wlodarek") | |
; lub | |
(struct-map person :first-name "Piotr", :last-name "Wlodarek") |
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
; odpowiednik_switch_case.clj | |
; cond to macro, zwróci nil jeśli żaden z warunków nie był spełniony | |
(cond | |
(< x 10) "less" | |
(> x 10) "more" | |
) |
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
; Jak wczytać plik clj do REPL-a? Jeśli nie ma przestrzeni nazw, | |
; wystarczy load lub load-file | |
(load-file "./temp.clj") ; z zadanej ścieżki | |
(load "temp") ; z CLASSPATH |
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
; Jak idiomatycznie wołać statyczne metody Javy? | |
(Character/isWhitespace 32) |
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
(use '[clojure.contrib.str-utils :only (str-join)]) | |
(str-join "-" ["ala" "ma" "kota"]) |
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
(defstruct account :id :balance) | |
(def accounts | |
(ref #{}) | |
) | |
(dosync ; force transaction | |
(alter accounts conj | |
(struct account 1 139.78) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Najbardziej praktyczne fragmenty kodu z początku książki "Programming Clojure"