Skip to content

Instantly share code, notes, and snippets.

@cohei
cohei / truthy-and-falsey.md
Last active July 13, 2016 13:14
List of Truthies and Falseys
@cohei
cohei / tap.hs
Last active June 25, 2020 16:32
Ruby's tap in Haskell
import Data.Functor
tap :: Functor f => a -> (a -> f b) -> f a
x `tap` action = x <$ action x
-- Only standard output
-- >>> print 1
-- 1
-- Standard output and return value
@cohei
cohei / toList.hs
Last active November 10, 2020 16:03
`catMaybes :: [Maybe a] -> [a]` の一般化
import Data.Foldable (Foldable, toList)
import Data.Functor.Compose (Compose(Compose))
import Data.Tree (Tree(Node))
flatten :: (Foldable f, Foldable g) => f (g a) -> [a]
flatten = toList . Compose
ex1 = [Just 1, Nothing, Just 2]
ex2 = Just [1,2]
ex3 = [[1],[2]]
@cohei
cohei / access_as_property.coffee
Last active August 29, 2015 14:04
Backbone のモデルでプロパティのように属性にアクセスする
# 参考: http://www.narrativescience.com/blog/automatically-creating-getterssetters-for-backbone-models/
buildProperties = (model) ->
buildGetter = (name) -> () -> model.get name
buildSetter = (name) -> (value) -> model.set name, value
for attribute in _.keys model.defaults
Object.defineProperty model, attribute,
get: buildGetter attribute
set: buildSetter attribute