Skip to content

Instantly share code, notes, and snippets.

View dawranliou's full-sized avatar

Daw-Ran Liou dawranliou

View GitHub Profile
@dawranliou
dawranliou / emoji.edn
Created September 18, 2019 19:26
Emoji for Clojure
{:hash "#️⃣"
:keycap_star "*️⃣"
:zero "0️⃣"
:one "1️⃣"
:two "2️⃣"
:three "3️⃣"
:four "4️⃣"
:five "5️⃣"
:six "6️⃣"
:seven "7️⃣"
@dawranliou
dawranliou / random-key.py
Created August 28, 2019 22:27
Generate 16-bytes server secret key
import secrets
import string
alphabet = string.ascii_letters + string.digits
print(''.join(secrets.choice(alphabet) for i in range(16)))
(defn newton
[f fprime tol max-iteration x]
(if (<= max-iteration 0)
nil
(let [y (f x)
yprime (fprime x)
x-new (- x (/ y yprime))]
(if (<= (Math/abs(- x-new x)) (* tol (Math/abs x-new)))
x-new
(newton f fprime tol (dec max-iteration) x-new)))))
package week6
import scala.io.Source
object mnemonics {
val in = Source.fromURL("https://raw.github.com/jpalmour/progfun/master/forcomp/src/main/resources/forcomp/linuxwords.txt")
val words = in.getLines.toList filter (word => word forall (chr => chr.isLetter))
val mnem = Map(
@dawranliou
dawranliou / translate.clj
Last active April 27, 2018 17:47
Functional Programming in Scala 6.5 in Clojure
(def words
(clojure.string/split-lines (slurp "https://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt")))
(def mnem
{\2 "ABC" \3 "DEF" \4 "GHI" \5 "JKL" \6 "MNO" \7 "PQRS" \8 "TUV" \9 "WXYZ"})
(def char-code
(into {} (for [[k vs] mnem
v vs]
[v k])))
(defn tri?
[n]
(= (last (take-while #(>= n %) tri-numbers)) n))
(tri? 10)
;; true
(tri? 11)
;; false
def is_tri(n):
for tri in tri_gen():
if tri > n:
return False
elif tri == n:
return True
else:
continue
is_tri(10)
(defn number-generator
([] (number-generator 0))
([i] (lazy-seq (cons i
(number-generator (inc i))))))
(def numbers (number-generator))
(take 6 numbers)
;; (0 1 2 3 4 5)
(defn tri-gen
([] (tri-gen 0 1))
([tri-num n]
(lazy-seq (cons tri-num
(tri-gen (+ tri-num n) (inc n))))))
(def tri-numbers (tri-gen))
(take 7 tri-numbers)
;; (0 1 3 6 10 15 21)
def tri_gen():
tri_num = 0
n = 1
while True:
yield tri_num
tri_num += n
n += 1
tri_numbers = tri_gen()
next(tri_numbers) # 0