Created
May 1, 2013 18:56
-
-
Save mike-neck/5497467 to your computer and use it in GitHub Desktop.
「Java 8を関数型っぽく使うためのおまじないをGroovyでやってみた」をGroovyにした
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
String.metaClass.define { | |
enclose = { | |
"[${delegate}]" as String | |
} | |
} | |
assert '[foo]' == 'foo'.enclose() | |
assert 'Foo' == 'foo'.capitalize() | |
assert '[Foo]' == 'foo'.capitalize().enclose() | |
String.metaClass.define { | |
middle = { | |
delegate.substring(delegate.size() / 3 as int, delegate.size() * 2 / 3 as int) | |
} | |
} | |
assert 'foo' == 'fizfoobar'.middle() | |
assert '[Foo]' == 'fizfoobar'.middle().capitalize().enclose() | |
String.metaClass.define { | |
sandwich = {enc -> | |
"${enc}${delegate}${enc}" as String | |
} | |
} | |
assert 'バイク川崎バイク' == '川崎'.sandwich('バイク') | |
/* | |
* カリー化 != 部分適用 | |
*/ | |
String.metaClass.define { | |
haskell = {pre -> | |
def item = delegate | |
return {post -> "$pre$item$post" as String} | |
} | |
haskell = {pre, post -> | |
"$pre$delegate$post" as String | |
} | |
} | |
assert 'ばいく川崎バイク' == '川崎'.haskell('ばいく')('バイク') | |
assert 'ばいく川崎バイク' == '川崎'.haskell('ばいく', 'バイク') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment