Created
October 10, 2012 07:40
-
-
Save gakuzzzz/3863797 to your computer and use it in GitHub Desktop.
Scalaで文字列を1つずつずらす
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Scalaz ver
def add(i: Int)(words: String) = words.map(_ + i - 'a' |> Stream.continually('a' to 'z').flatten)
確かに! さらに簡潔ですし。 to, until を失念してました。ありがとうございます!
ごるふ
def add(i:Int)=(_:String).map({(_:Char)+i-'a'}andThen Stream.continually('a'to'z').flatten)
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
Range.inclusive('a', 'z')
を'a' to 'z'
にしたら最後のtoCharはいらないですね