Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / truthy-and-falsey.md
Last active July 13, 2016 13:14
List of Truthies and Falseys
def distribute(n, xs)
xs.each_slice(n).map { |array| l = array.length ; array + [nil]*(n-l) }.transpose.map(&:compact)
end
distribute(3, (1..7)) #=> [[1, 4, 7], [2, 5], [3, 6]]
@cohei
cohei / auto-install-dotfiles.md
Last active July 29, 2020 04:20
ドットファイルを GitHub に置き、インストールを自動化した話

ドットファイル、ご存じでしょうか。 UNIX 系のシステムの、主にホームディレクトリにある . (ドット) から始まる名前の設定ファイルのことをいいます。 .bashrc とか .emacs.d/ とかありますよね。だんだん手になじむ設定がたまってきて、複数の環境で使い回したくなってくるわけです。そこで、ドットファイルを GitHub に置いている人をよく見かけます。

私も置いています。何もしなければ、各環境でこのレポジトリをクローンしてシンボリックリンクを張ったりするわけです。めんどくさいですね。

そこで、今日のネタは install.sh です。このようなスクリプトを書いておけば、

curl https://raw.githubusercontent.com/cohei/dotfiles/master/install.sh | bash
@cohei
cohei / aho.txt
Created May 3, 2015 18:38
3のつく数字と3の倍数でアホになる
ghcによるアホ率の計算
10^1 0.3
10^2 0.45
10^3 0.513
10^4 0.5625
10^5 0.60633
10^6 0.645705
10^7 0.6811353
10^8 0.71302185
10^9 0.741719673
import Control.Arrow (first)
import Control.Concurrent (threadDelay)
import Control.Monad (forM_, replicateM)
import Data.List (isSuffixOf, inits)
import System.Environment (getArgs)
import System.Random (Random(random, randomR, randoms), newStdGen)
main :: IO ()
main = do
args <- getArgs
@cohei
cohei / eru.hs
Last active September 15, 2016 07:30
千反田える http://blog.hiyamugi.net/2012/10/31/0001 より。漢数字などはサボった。
#!/usr/bin/env stack
-- stack runghc --install-ghc --resolver=lts-7.0
data Tanda = Tanda Int Char
instance Show Tanda where
show (Tanda i c) = show i ++ "反田" ++ [c]
next :: Tanda -> Tanda
next (Tanda i c) = Tanda (succ i) (if c == 'Z' then 'A' else succ c)