Skip to content

Instantly share code, notes, and snippets.

View craftybones's full-sized avatar

Srijayanth Sridhar craftybones

  • Bangalore, India
View GitHub Profile
#include <stdlib.h>
#include <stdio.h>
#include "filter.h"
IntArray *create_int_array_from(int *values, int length)
{
IntArray *array = (IntArray *)malloc(sizeof(IntArray));
array->values = (int *)malloc(sizeof(int) * length);
array->length = length;
for (size_t i = 0; i < array->length; i++)
(defn take-while-adjacent [pred]
(fn [rf]
(let [prev (volatile! ::none)]
(fn ([] (rf))
([result] (rf result))
([result input]
(let [pv @prev]
(if (or (identical? pv ::none) (pred @prev input))
(do
(vreset! prev input)
const repeatedly = function* (x) {
while(true) {
yield x;
}
}
const cycle = function* (list) {
while(true) {
for (x of list) {
yield x;
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))