Skip to content

Instantly share code, notes, and snippets.

View otofu-square's full-sized avatar

Hidemi Yukita otofu-square

  • Knowledge Work Inc.
  • Tokyo
View GitHub Profile
@otofu-square
otofu-square / throwIf.ts
Created September 21, 2017 06:54
Throw error if predicate function returns true
import { curry } from "ramda";
export const throwIf = curry(
(predicate: (_: any) => boolean, error: Error, value: any) => {
if (predicate(value)) {
throw error;
} else {
return value;
}
},
@otofu-square
otofu-square / hereDocument.ts
Last active September 4, 2017 05:26
Implementation of Ruby's here document in TypeScript
// 文字列の行頭にあるスペースのサイズを取得する
const getSpaceLengthAtBeginningOfLine = (str: string) =>
str.match(/^(\s*)/)![1].length;
// 末尾の空行を削除する
const trimTailSpaces = (str: string) => str.match(/(.*?)(\s*)$/)![1];
// Number の配列から最小の値を取りだす
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
const getMinOfArray = (arr: number[]) => Math.min.apply(null, arr);
@otofu-square
otofu-square / check-object-values.ts
Last active August 30, 2017 06:09
Dynamic check types of the object values in TypeScript with ramda
import {
all,
curry,
equals,
isNil,
mapObjIndexed,
pipe,
propSatisfies,
values,
} from "ramda";
@otofu-square
otofu-square / compose.rb
Last active November 30, 2018 08:04
Implement compose function in Ruby
require 'pp'
# block/proc/lambda のおさらい①
#
# - block とは
# メソッドに「処理」を渡したい時に使うもの
def hoge(&block)
block("World")
end
@otofu-square
otofu-square / fibonacchi.clj
Last active June 30, 2017 08:10
Fibonacchi in clojure
(defn fibonacchi
[n]
(condp = n
0 1
1 1
(+ (fibonacchi (- n 1)) (fibonacchi (- n 2)))))
@otofu-square
otofu-square / fizzbuzz.clj
Last active June 30, 2017 08:10
Fizzbuzz in clojure
(defn fizzbuzz
[n]
(cond
(and (= (rem n 3) 0) (= (rem n 5) 0)) "fizzbuzz"
(= (rem n 5) 0) "buzz"
(= (rem n 3) 0) "fizz"
:else (.toString n)))
@otofu-square
otofu-square / quadratic_equation.rb
Last active March 6, 2017 16:04
Quadratic Equations
class QuadraticEquation
attr_reader :x
def initialize(a:, b:, c:)
@a = a.to_f
@b = b.to_f
@c = c.to_f
@x = result
end
@otofu-square
otofu-square / FakeQuickSort.hs
Created February 17, 2017 05:43
Haskell sample code
qsort [] = []
qsort (p:xs) = qsort lt ++ [p] ++ qsort gteq
where lt = [x | x <- xs, x < p]
gteq = [x | x <- xs, x >= p]
@otofu-square
otofu-square / convert_music.rb
Last active October 28, 2015 17:49
Apple Loss Less → MP3 translation
require 'streamio-ffmpeg'
require 'yaml'
INPUT_PATH = "~/変換元ディレクトリ"
OUTPUT_PATH = "~/変換後のファイルを保存するディレクトリ"
# YAMLから変換する曲のアーティスト一覧を取得
# example: artists.yml
# - Bireli\ Lagrene
# - John\ Pizzarreli