Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active December 18, 2015 00:29
Show Gist options
  • Save xuwei-k/5696494 to your computer and use it in GitHub Desktop.
Save xuwei-k/5696494 to your computer and use it in GitHub Desktop.

collection

underscoreScala
each(list, iterator, [context]
forEach
foreach
map(list, iterator, [context])
collect
map
reduce(list, iterator ,memo, [context])
inject, foldl
memoを渡せばfoldLeft、memoなしならreduceLeft
reduceRight(list, iterator ,memo, [context])
foldr
memoを渡せばfoldRight、memoなしならreduceRight
find(list, iterator, [context])
detect
Scalaの場合Optionだが、underscoreでは一致するものが無ければndefinedが返る
filter(list, iterator, [context])
select
filter
where(list, properties) なし。あえて言うなら、返る型や目的としてはfilterが近いだろうか
findWhere(list, properties) underscoreのwhereの最初に見つかった1つを返すversion。これも上記と同じく直接対応するものない
reject(list, iterator, [context]) filterNot
every(list, iterator, [context])
all
forall
some(list, [iterator], [context])
any
exists。2つめの引数もオプションになってるのはJSっぽい
contains(list, value)
include
contains
invoke(list, methodName, [*arguments]) なし。そもそもコレクションの操作じゃないような。
pluck(list, propertyName) なし
max(list, [iterator], [context]) 引数1つならmax。引数2つならmaxBy
min(list, [iterator], [context]) 引数1つならmin。引数2つならminBy
sortBy(list, iterator, [context]) sortBy
groupBy(list, iterator, [context]) groupBy
countBy(list, iterator, [context]) groubByしてmapValues(_.size)
shuffle(list) scala.util.Random.shuffle
toArray(list) toArray
size(list) size

array

underscoreScala
firsthead。引数2つのものはtake
initialinit。引数2つだと、dropRight
lastlast。引数2つだと、takeRight
rest
tail,drop
tail。引数2つだと、drop
compactなし
flattenflattenに近いが、Rubyのflattenのように、デフォルトでは複数のネストを全部flatにする
withoutfilterNot(_.contains()))。もしくはSetの-
unionSetのunion
intersectionSetのintersect
differencediff
uniq
unique
distinct
zipListなどのzip。もしくは Tuple2やTuple3のzipped
objectなし
indexOfindexOf
lastIndexOflastIndexOf
sortedIndexなし?
rangeRangeの各種関数
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment