Skip to content

Instantly share code, notes, and snippets.

View YusukeHosonuma's full-sized avatar
🏠
Working from home

Yusuke Hosonuma YusukeHosonuma

🏠
Working from home
View GitHub Profile
@YusukeHosonuma
YusukeHosonuma / map_filter.hs
Created May 8, 2016 13:19
Haskell - implement map and filter functions by recursion and foldr versions.
-- | recursion version
map' :: (a -> b) -> [a] -> [b]
map' _ [] = []
map' f (x:xs) = f x : map' f xs
filter' :: (a -> Bool) -> [a] -> [a]
filter' _ [] = []
filter' f (x:xs)
| f x = x : (filter' f xs)
@YusukeHosonuma
YusukeHosonuma / unix_like.hs
Created May 8, 2016 14:27
Haskell - UNIX's pipe like style for functional composition
-- | すごいHaskell楽しく学ぼう版
-- replicate 2 . product . map (*3) $ zipWith max [1, 2] [4, 5]
--
-- | オリジナルパイプ版(UNIXライク)
-- zipWith max [1, 2] [4, 5] > (map (*3) | product | replicate 2)
--
(|) :: (a -> b) -> (b -> c) -> (a -> c)
g | f = \x -> f $ g x
(>) :: a -> (a -> b) -> b
@YusukeHosonuma
YusukeHosonuma / Cookie.js
Last active June 26, 2016 14:34
Cookie Clicker v2.0 - Automatically click for BigCookie and GoldenCookie.
@YusukeHosonuma
YusukeHosonuma / ViewController.swift
Created June 5, 2016 12:44
RxSwift: Two UITextView text count.
import UIKit
import RxSwift
import RxCocoa
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var textView2: UITextView!
@YusukeHosonuma
YusukeHosonuma / main.swift
Created July 17, 2016 13:12
[Swift] Measure Set's contains() performance
func time(title: String, execute: () -> ()) {
let now = NSDate()
execute()
let interval = NSDate().timeIntervalSinceDate(now)
print("\(title): \(interval)")
}
var s = Set<String>()
(0..<1000).forEach{ s.insert(String($0)) }
@YusukeHosonuma
YusukeHosonuma / CSVFormatter.hs
Last active August 22, 2016 00:45
CSVFormatter.hs
-- CSVFormatter.hs
{-
Input:
Name,Birthday,Height
Suzukaze Aoba,2/2,149cm
Ko Yagami,8/2,164cm
Rin Toyama,12/3,158cm
@YusukeHosonuma
YusukeHosonuma / reduceDictionary.swift
Last active August 27, 2016 12:27
Array reduce to Dictionary helper function
// Definition
extension Array {
func reduceDictionary<Key: Hashable, Value>(extractKeyValue: Element -> (Key, Value)) -> [Key: Value] {
return reduce([Key: Value]()) {
var dictionary = $0.0
let (key, value) = extractKeyValue($0.1)
dictionary[key] = value
return dictionary
}
}
@YusukeHosonuma
YusukeHosonuma / BattleShip.hs
Last active September 15, 2016 14:36
Functional Haskell - 02
type Distance = Double
data Position = Position Double Double deriving (Show)
data Ship = Ship { position :: Position
, firingRange :: Distance
, unsafeRange :: Distance } deriving (Show)
type Region = Position -> Bool
@YusukeHosonuma
YusukeHosonuma / ToArray.hs
Created September 20, 2016 11:41
Array Literal Generator
#!/usr/bin/env stack
-- stack --resolver lts-7.0 --install-ghc runghc --package turtle
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (pack, unpack)
import Turtle
joinM :: (Monoid m) => m -> [m] -> m
joinM separator = foldl1 (\a b -> a `mappend` separator `mappend` b)
@YusukeHosonuma
YusukeHosonuma / main.swift
Last active October 16, 2016 15:04
Class and Struct memory allocation in Swift 3.0
// Class and Struct memory allocation in Swift 3.0
func adr<T>(_ title: String, _ x: inout T) {
withUnsafePointer(to: &x) {
print(title, $0)
}
}
class C0 {}
class C1 {