Skip to content

Instantly share code, notes, and snippets.

View dawranliou's full-sized avatar

Daw-Ran Liou dawranliou

View GitHub Profile
>>> range(10)
range(0, 10)
# Use list() to evaluate the range object
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def number_generator():
i = 0
while True:
yield i
i += 1
numbers = number_generator()
next(numbers) # 0
next(numbers) # 1
next(numbers) # 2
next(numbers) # 3
from itertools import count
numbers = count()
next(numbers) # 0
next(numbers) # 1
next(numbers) # 2
next(numbers) # 3
#...
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
(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)
(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)
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 tri?
[n]
(= (last (take-while #(>= n %) tri-numbers)) n))
(tri? 10)
;; true
(tri? 11)
;; false
@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])))
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(