Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Created October 10, 2012 07:40
Show Gist options
  • Save gakuzzzz/3863797 to your computer and use it in GitHub Desktop.
Save gakuzzzz/3863797 to your computer and use it in GitHub Desktop.
Scalaで文字列を1つずつずらす
scala> def add(i: Int)(words: String) = words.map({x: Char => x + i - 'a'} andThen Stream.continually(Range.inclusive('a', 'z')).flatten andThen {_.toChar})
add: (i: Int)(words: String)String
scala> add(2)("abcdz")
res16: String = cdefb
@halcat0x15a
Copy link

Scalaz ver

def add(i: Int)(words: String) = words.map(_ + i - 'a' |> Stream.continually('a' to 'z').flatten)

@gakuzzzz
Copy link
Author

確かに! さらに簡潔ですし。 to, until を失念してました。ありがとうございます!

@xuwei-k
Copy link

xuwei-k commented Oct 10, 2012

ごるふ

def add(i:Int)=(_:String).map({(_:Char)+i-'a'}andThen Stream.continually('a'to'z').flatten)

@gakuzzzz
Copy link
Author

compose だと型推論が効くっぽい

def add(i: Int)=(_:String).map(Stream.continually('a'to'z').flatten compose {_+i-'a'})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment