-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclojure-learning-template.clj
More file actions
82 lines (63 loc) · 1.49 KB
/
clojure-learning-template.clj
File metadata and controls
82 lines (63 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(ns learning.core
"A simple learning project for Clojure development in Neovim"
(:require [clojure.string :as str]))
;; Basic data types and operations
(def greeting "Hello, Clojure!")
(def numbers [1 2 3 4 5])
(def person {:name "Alice" :age 30 :city "Portland"})
;; Functions
(defn add [a b]
(+ a b))
(defn greet [name]
(str "Hello, " name "!"))
(defn square [x]
(* x x))
;; Higher-order functions
(defn apply-to-all [f xs]
(map f xs))
(defn double-all [xs]
(map #(* 2 %) xs))
;; Conditionals and pattern matching
(defn classify-number [n]
(cond
(< n 0) "negative"
(zero? n) "zero"
(pos? n) "positive"))
;; Sequences and collections
(defn sum-of-squares [xs]
(->> xs
(map square)
(reduce +)))
;; Recursion example
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (dec n)))))
;; Testing functions (run these in REPL: <space><space>ee)
(comment
;; Evaluate these lines one by one with <space><space>ee
;; Basic operations
(add 2 3)
(greet "World")
(square 5)
;; Working with sequences
(map square numbers)
(filter even? numbers)
(reduce + numbers)
;; Working with maps
person
(:name person)
(assoc person :age 31)
;; Using helper functions
(double-all [1 2 3 4 5])
(classify-number -5)
(classify-number 0)
(classify-number 10)
;; Recursion
(factorial 5)
;; List comprehension
(for [x [1 2 3] y [10 20]] [x y])
;; String operations
(str/upper-case greeting)
(str/split greeting #", ")
)