Upgrade to Pro — share decks privately, control downloads, hide ads and more …

関数型プログラミング

まりも
September 24, 2024

 関数型プログラミング

関数型プログラミングをその基本の宣言型気泡を中心に解説し、またオブジェクト指向言語での利用のコツなども説明します。

まりも

September 24, 2024
Tweet

More Decks by まりも

Other Decks in Programming

Transcript

  1. 高階関数 char* data[] = {"Apple", "Microsoft", "Google", "Oracle", "Dell"}; char

    deleted = 'O'; resultNum = 0; for(i = 0; i < dataNum; i++){ /* 頭文字がbad以外のもののみを結果として抽出 */ if(data[i][0] != deleted){ result[resultNum++] = data[i]; } } /*文字列数でソート*/ for(i = 0; i < resultNum; i++){ for(j = i + 1; j < resultNum; j++){ if(strlen(result[j]) < strlen(result[i])){ tmp = result[i]; result[i] = result[j]; result[j] = tmp; } } } C var data = "Apple" :: "Microsoft" :: "Google" :: "Oracle" :: "Dell" :: Nil var deleted = "O" var result = data filter(!_.startsWith(deleted)) sortWith(_.size < _.size) Scala
  2. 高階関数 data = %w(Apple Microsoft Google Dell Oracle) deleted =

    "O" result = data .find_all{|s| !s.start_with? deleted} .sort{|l, r| l.size <=> r.size} Ruby var data = new[] { "Apple", "Microsoft", "Google", "Oracle", "Dell" }; var deleted = "O"; var result = from s in data where !s.StartsWith(deleted) orderby s.Length select s; var data = new[] { "Apple", "Microsoft", "Google", "Oracle", "Dell" }; var deleted = "O"; var result = data .Where(s => !s.StartsWith(deleted)) .OrderBy(s => s.Length); Dim data = New String() {"Apple", "Microsoft", "Google", "Oracle", "Dell"} Dim deleted = "O“ Dim result = From s In data Where Not s.StartsWith(deleted) Order By s.Length Select s C# C# (メソッド構文) Visual Basic $data = array('Apple', 'Microsoft', 'Google', 'Oracle', 'Dell'); $deleted = 'O'; $result = array_filter($data, function($s) use ($deleted){return substr($s, 0, 1) != $deleted;}); usort($result, function($l, $r){return strlen($l) - strlen($r);}); PHP List<String> data = Arrays.asList("Apple", "Microsoft", "Google", "Oracle", "Dell"); String deleted = "O"; Stream<String> result = data.stream() .filter(s -> !s.startsWith(deleted)) .sorted((l, r)->l.length() - r.length()); Java data = ['Apple', 'Microsoft', 'Google', 'Dell', 'Oracle'] deleted = 'O' result = [x for x in sorted(data, key=len) if not x.startswith(deleted)] Python @data = qw/Apple Microsoft Google Dell Oracle/; $deleted = "O"; @result = sort {length $a <=> length $b} (grep /^[^$deleted].*/, @data); Perl
  3. 高階関数 (def data ["Apple" "Microsoft" "Google" "Oracle" "Dell"]) (def delete

    ¥O) (sort #(< (count %)(count %2)) (filter #(not (= (first %) delete)) data)) Clojure data = ['Apple', 'Microsoft', 'Google', 'Oracle', 'Dell'] deleted = 'O' result = (s for s in data when s.charAt(0) != deleted) result.sort (l, r) -> l.length - r.length CoffeeScript var data = ['Apple', 'Microsoft', 'Google', 'Oracle', 'Dell']; var deleted = 'O'; var result = data.filter((s) => !s.startsWith(deleted); result.sort((l, r) => l.length - r.length); Dart let data = ["Apple"; "Microsoft"; "Google"; "Oracle"; "Dell"] let deleted = "O" let result = data |> List.filter(fun s -> not(s.StartsWith(deleted))) |> List.sortBy(fun s -> s.Length) F# |data deleted result| data ← #('Apple' 'Microsoft' 'Google' 'Dell' 'Oracle'). deleted ← 'O'. result ← data select:[:s | (s beginsWith: deleted) not]. result ← result sortBy:[:l :r | (l size) < (r size)]. Smalltalk var data = "Apple" :: "Microsoft" :: "Google" :: "Oracle" :: "Dell" :: Nil var deleted = "O" var result = data filter(!_.startsWith(deleted)) sortWith(_.size < _.size) Scala JavaScript let data = ['Apple', 'Microsoft', 'Google', 'Oracle', 'Dell']; let deleted = 'O'; let result = data.filter(s=>s.charAt(0) != deleted); result.sort((l, r)=>l.length - r.length); var data = ['Apple', 'Microsoft', 'Google', 'Oracle', 'Dell'] var deleted = 'O' var result = data.filter(s=>s.charAt(0) != deleted) result.sort((l, r)=>l.length - r.length) TypeScript
  4. 高階関数 let data = ["Apple", "Microsoft", "Google", "Oracle", "Dell"] let

    deleted = "O“ var result = data.filter({ !$0.hasPrefix(deleted) }) result.sort({count($0) < count($1)}) Swift let data = ["Apple", "Microsoft", "Google", "Oracle", "Dell"]; let deleted = "O"; let mut result : Vec<&&str> = data.iter().filter(|s| !s.starts_with(deleted)).collect(); result.sort_by(|l, r|l.chars().count().cmp(&r.chars().count())); Rust auto data = ["Apple", "Microsoft", "Google", "Oracle", "Dell"]; const deleted = 'O'; auto result = data.filter!(s => s[0] != deleted).array; result.sort!((l, r)=>l.length < r.length); D val data = array("Apple", "Microsoft", "Google", "Oracle", "Dell") val deleted = 'O' val result = data.filter{it[0] != deleted} sortBy{it.length} Kotlin NSArray *data = [NSArray arrayWithObjects:@"Apple", @"Microsoft", @"Google", @"Oracle", @"Dell", nil]; NSString *deleted = @"O"; NSIndexSet *filteredIndexes = [data indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop){return (BOOL)![obj hasPrefix:deleted];}]; NSArray *result = [[data objectsAtIndexes:filteredIndexes] sortedArrayUsingComparator: ^(id l,id r){ NSNumber *lLength = [NSNumber numberWithUnsignedInteger:[l length]]; NSNumber *rLength = [NSNumber numberWithUnsignedInteger:[r length]]; return [lLength compare:rLength]; }]; Objective-C $data = 'Apple', 'Microsoft', Google', 'Oracle', 'Dell' $deleted = 'O' $result = $data | ?{$_ -notlike "$deleted*"} | Sort-Object Length Powershell vector<string> data = { "Apple", "Microsoft", "Google", "Oracle", "Dell" }; auto deleted = 'O'; auto newEnd = remove_if(data.begin(), data.end(), [&](auto s) { return s[0] == deleted; }); data.erase(newEnd, data.end()); sort(data.begin(), data.end(), [](auto l, auto r) { return l.size() < r.size(); }); C++
  5. IF文やFOR文を使わない const data = [1, 2, 3, 4, 5, 6,

    7, 8, 9, 10] .filter(i => i % 3 == 0) .map(i => i * i)
  6. いっそLINQ.JSで const data = Enumerable.range(-10, 20) .where(i => i %

    3 == 0) .orderByDescending(i => i % 5) .select(i => i * i) .toArray(); SQLで書けるとは大体書けるよう設計されています