Skip to content

Instantly share code, notes, and snippets.

import Data.Char
import Control.Applicative
import Data.Maybe
isbn :: String -> Bool
isbn s = isValid && hasValidChecksum
where
isbnPattern = "D-DDD-DDDDD-X"
checksum = sum . zipWith (*) [10, 9..1] . mapMaybe fromJust $ vals
addName counter names name = do
i <- readTVar counter
ns <- readTVar names
orElse addName' $ return ()
where
addName' = do
guard $ any ((== name) . snd) ns
writeTVar names ((i, name) : ns)
writeTVar counter (i + 1)
(defn- valid-pattern [s] (boolean (re-matches #"\d-\d{3}-\d{5}-[\dX]" s)))
(defn- valid-checksum [s]
(let [csum (->> s
(map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9 \X 10})
(keep identity)
(map * (range 10 0 -1))
(reduce +))]
(zero? (rem csum 11))))
(def isbn? (every-pred valid-pattern valid-checksum))
; type annotation, read String -> Maybe String
(>fdef number [s] [string? => (? string?)])
(def number (comp second (partial re-find #"^1?([2-9]\d{2}[2-9]\d{6})$") #(str/replace % #"[\D]" "")))
(>defn number
"Cleans up NANP-style Phone Numbers."
[s]
[string? => (? string?)]
(let [only-digits #(clojure.string/replace % #"[\D]" "")
nanp-match #(re-find #"^1?([2-9][0-9]{2}[2-9][0-9]{6})$" %)]
(->> s only-digits nanp-match second)))
(s/def ::phrases (s/map-of number? string?))
(def ^:private phrases {3 "Pling" 5 "Plang" 7 "Plong"})
(s/fdef raindrops
:args (s/cat :n number?)
:ret string?)
(defn raindrops
"Returns number as string unless it is divisible by 3, 5 or 7, in which case it returns
the concatenation of the words Pling (3), Plang (5) and Plong (7) for the divisible cases."
[n]
raindrops :: Int -> String
raindrops n = tests id (show n)
where
tests = test 3 "Pling" . test 5 "Plang" . test 7 "Plong"
test d s x | n `mod` d == 0 = const (s ++ x "")
| otherwise = x
{-# LANGUAGE LambdaCase #-}
data Planet = Earth | Mercury | Venus | Mars | Jupiter | Saturn | Uranus | Neptune
deriving (Eq)
ageOn :: Planet -> Float -> Float
ageOn = flip (/) . (earthSecondsPerYear *) . earthSecondsFor
where
earthSecondsPerYear = 31557600
earthSeconds =
@lumie1337
lumie1337 / etl.clj
Last active September 20, 2019 06:50
(ns etl.core
(:require [clojure.string :as str]))
(defn transform [source]
(into {}
(for [[score cs] source
c cs]
[(str/lower-case c) score])))
;; and with macro abstractions

Usage

  • Create enqueue.sh and consumer.sh with the below contents.
  • start consumer.sh in the shell where you want to run commands
  • use enqueue.sh echo hello to run echo hello in the other shell

Optionally:

  • use environment variable FIFO=./queue2 consumer.sh to use a different queue