Skip to content

Instantly share code, notes, and snippets.

View craftybones's full-sized avatar

Srijayanth Sridhar craftybones

  • Bangalore, India
View GitHub Profile
class Unit a where
standard :: a
class RatioUnit a where
standardFactor :: a -> Float
toStandard :: (RatioUnit a) => a -> Float -> Float
toStandard a x = (standardFactor a) * x
data Measure b = Measure Float b deriving (Show)
(def code '(+ 2 (+ 3 4)))
(defn wrangle [code]
(loop [c code
newc '()]
(if (seq? (last c))
(recur (last c) (conj newc (butlast c)))
(conj newc (butlast c) (last c) '->>))))
; user=> (wrangle code)
(def suits (partial map :suit))
(def ranks (partial map :rank))
(def by-rank (partial group-by :rank))
(defn largest-group [hand]
(->> hand
by-rank
vals
(apply max-key count)))

Idiomatic Katas

Implement the following katas in a language of your choice. The objective is to try to find "canonical" or "idiomatic" solutions to the problems

  1. Find max number in nested list of numbers [[1 2] [3 4]]
  2. Validate date
  3. Check if number is prime
  4. Generate a prime number series
  5. Transpose of a matrix
(defn divisible-by? [x y]
(zero? (rem y x)))
(defn leap? [year]
(condp divisible-by? year
400 true
100 false
4 true
false))
import Data.List
groupN :: Int -> [a] -> [[a]]
groupN _ [] = []
groupN 0 _ = undefined
groupN n x = take n x : groupN n (drop n x)
transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose x = map head x : (transpose (map tail x))
(require '[clojure.string :as cstr])
(defn pendulum [from to step]
(concat (range from to step) (range to (dec from) (- step))))
(defn rev-pendulum [from to step]
(concat (range to from (- step)) (range from (inc to) step)))
(defn line [max-size size]
(rev-pendulum (inc (- max-size size)) max-size 1))
(def remove-zeroes (partial remove zero?))
(def partition-number-sequences (partial partition-by identity))
(def pair-up (partial partition-all 2))
(def pair-partitions (partial mapcat pair-up))
(def sum-of (partial apply +))
(def sum-pairs (partial map sum-of))
(defn concat-zeroes
[coll]
(concat coll (repeat 0)))
import qualified Data.Set as S
import qualified Data.List as L
import qualified Data.Map as M
type Move = Int
type Moves = S.Set Move
chunk :: [a] -> Int ->[[a]]
chunk [] n = []
chunk l n = [(take n l)] ++ (chunk (drop n l) n)
const jdenticon = require('jdenticon');
const fs = require('fs');
const size=250;
let names=fs.readFileSync("names","utf8").split("\n");
names.forEach(name=>{
fs.writeFileSync(`${name}.png`,jdenticon.toPng(name,size));
})