Last active
August 29, 2015 14:13
-
-
Save mdk-aza/1cbf33d842cdd90d1723 to your computer and use it in GitHub Desktop.
This file contains 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
/// <reference path='typings/tsd.d.ts' /> | |
//関数定義 | |
var countFruitsFromLines = (list:string[]) => | |
_.chain(list) | |
// 文字列分割の処理を配列の中身にそれぞれ行い、配列を新たに生成して返す。 | |
.map((_list:string[]) => _list.split(' ')) | |
// 階層的リストに対しても全て展開して、階層がない配列に変換して返す。 | |
.flatten() | |
// 配列の要素をカウントし、それぞれの要素とその出現回数をキー値として格納したオブジェクトを返す。 | |
.countBy() | |
// ラッパーオブジェクトからchainメソッドの処理結果を抽出して返す。 | |
.value(); | |
var list:string[] = ["apple banana", "orange apple mango", "kiwi papaya orange", "mango orange muscat apple"]; | |
console.log(countFruitsFromLines(list)); | |
//実行結果 | |
//{apple: 3, banana: 1, orange: 3, mango: 2, kiwi: 1, muscat: 1, papaya: 1} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment