Skip to content

Instantly share code, notes, and snippets.

View cesare's full-sized avatar

SAWADA Tadashi cesare

View GitHub Profile
module MaxSubseq (maxSubseq) where
import Prelude hiding (seq, sum)
data Seq = Seq { seq::[Double], sum::Double } deriving Show
instance Eq Seq where
(Seq _ x) == (Seq _ y) = x == y
instance Ord Seq where
(Seq _ x) <= (Seq _ y) = x <= y
class Celebrity
attr_reader :matrix
def initialize(rows)
@matrix = rows
end
def knows?(from, to)
@matrix[from][to] == 1
end
import Data.List ((\\))
import qualified Data.Map as Map
prune :: Ord k => Map.Map k k -> Map.Map k k
prune m = _prune m orphans
where
_prune m [] = m
_prune m os = prune $ foldl (flip Map.delete) m os
orphans = (Map.keys m) \\ (Map.elems m)
@cesare
cesare / polynomials.ex
Last active January 12, 2016 17:08
5.2 polynomials
defmodule Chapter5 do
def polynomial2(as, x) do
Stream.iterate(1, &(&1 * x))
|> Enum.zip(as)
|> Enum.map(fn {x, a} -> x * a end)
|> Enum.sum
end
def polynomial3(as, x) do
Enum.reverse(as) |> Enum.reduce(&(&2 * x + &1))
@cesare
cesare / elm.md
Last active September 17, 2015 04:48

社内勉強会 Elm 篇

Elm

  • AltJS (?)
  • 関数型
  • Haskell っぽい構文
    • モナドとか出てこない
    • 遅延評価はない
  • Reactive / FRP
require 'sudden_death'
module Lita
module Handlers
class Suddendeath < Handler
route /^(突然の.*)/, :sudden_death, command: false
def sudden_death(response)
response.reply response.match_data[0].sudden_death
end
TAX_RATE = 1.08
# 「1.08 を掛けて→小数点以下を切り捨てる」という演算で target を超えない最大の数値を求める
# 戻値 [t: Fixnum, d: Fixnum, i: Fixnum, s: Fixnum]
#
# t: 元々の税込み価格 (= target)
# d: 求める税抜き価格
# i: 計算結果の税抜き価格に税を乗せた価格
# s: 差分 (t - i)
#
// http://qiita.com/t_uda/items/1969e09a970d71e4cfd6
function hoge(x) {
switch (true) {
case x < 0:
console.log(x + " は自然数ではありません.");
break;
case x === 0:
console.log("ここでは 0 は自然数です.");
break;
# Example Answer to http://qiita.com/jnchito/items/c4a56046be1096c19b1c
def count_by_word(str)
raise ArgumentError, 'str should be a string' unless str.is_a? String
str.split.reduce(Hash.new(0)) {|rs, w| rs.merge(w => rs[w] + 1) }
end
describe "count_by_word" do
let(:results) { count_by_word(string) }