Ruby 2.5.0 was recently released. Ruby has sequence predicates such as all?, none?, one? and any? which take a block and evaluate that by passing every element of the sequence to it. 1if queries.any? { |sql| /LEFT OUTER JOIN/i =~ sql } 2logger.log "Left outer join detected" 3endCopy Ruby 2.5 allows using a shorthand for this by passing a pattern argument. Internally case equality operator(===) is
Ruby 2.2 で追加されたメソッドまとめをみてた。 New Methods in Ruby 2.2 Enumerable#slice_whenのユースケースでEnumerable内での各要素出現数をカウントするのがステキだったので、2.2以前も含めてどう書けるパターンあるんだっけ?をメモ。 カウント対象のArrayを生成 labels = ["aaa", "bbb", "ccc"] ary = Array.new(20) { labels.sample } puts ary.join ", " # bbb, aaa, bbb, bbb, bbb, aaa, ccc, aaa, aaa, bbb, ccc, ccc, bbb, bbb, aaa, aaa, ccc, bbb, aaa, aaa
Enumeration by definition is “the action of mentioning a number of things one by one.” In programming, instead of mentioning, we choose any action we may want to perform, whether it simply be printing out the item to a display or performing some sort of selection and/or transformation on the item. In programming, we can perform many ways to select and process a collection at one time by chaining o
Posted on May 28, 2017 by Tina Wuest There is a terrible cycle which exists in the lifecycle of functional programmers. Monads are confusing at first so the programmer will read many articles explaining how monads are actually easy—which will fail to achieve any pedagogical goals due to the fact that the article is filled with self-referential language, leaving the programmer even more confused. E
var names = new List<string>{"Taro", "Jiro", "Saburo", "Shiro", "Goro", "Rokuro"}; var result = new List<string> (); for(int i = 0; i < names.Length; i++) { result.Add (string.Format("index{0} {1}", i, names[i])); } for文やListのインデクサーを用いていますね。これをLINQで書き換えてスッキリさせてみせましょう。抽出はWhereというメソッド、射影はSelctというメソッドを使えばいいですね。 ですが上のコードでは、抽出にも射影にもインデックスを用いてます。for文で定義し、ループの度にインクリメントされるインデックスiは使えるのでしょうか。 ちょっと前に、LINQ、その
この投稿は、C# Advent Calendar 2014 の14日目の記事です。 IEnumerableにまつわるアレやアレ こんにちは! いつもはAsyncとLINQの事をしゃべったり書いたりしています。「列挙可能から完全なるモノまで – IEnumerableの探索」というお題で書いてみようと思います。 .NETでは、LINQにまつわるインターフェイスがかなり多く含まれています。その中でも、IEnumerableインターフェイスの継承グラフに存在する様々なインターフェイスを、どのように使い分けるべきか、と言うのが分かりにくいかも知れません。沢山のインターフェイスが定義されているのは、歴史的な事情もあります。 LINQ to ObjectやLINQ to Entitiesなど、一般的に使用されているLINQの背景には、「列挙可能である」という性質があります。この事は、以前にLINQの勉
In Ruby, we commonly use uniq method on an array to fetch the collection of all unique elements. But there may be cases where we might need elements in a hash by virtue of uniqueness of its values. Let's consider an example of countries that have hosted the Olympics. We only want to know when was the first time a country hosted it. 1 2# given object 3{ 1896 => 'Athens', 4 1900 => 'Paris', 5 1904 =
In https://bugs.ruby-lang.org/issues/12217#note-3, Akira Tanaka, mentions that the default argument to sum is 0. This creates problems with non-numeric summations (e.g. strings). I would like to suggest using the first enumerable value. This would make the method more flexible. It also makes it behave more like reduce. I think using the initial value in the enumerable is less surprising than using
It is a common use case to calculate sum of the elements of an array or values from a hash. 1 2[1, 2, 3, 4] => 10 3 4{a: 1, b: 6, c: -3} => 4 5 Copy 1 2> [1, 2, 3, 4].sum 3 #=> 10 4 5> {a: 1, b: 6, c: -3}.sum{ |k, v| v**2 } 6 #=> 46 7 8> ['foo', 'bar'].sum # concatenation of strings 9 #=> "foobar" 10 11> [[1], ['abc'], [6, 'qwe']].sum # concatenation of arrays 12 #=> [1, "abc", 6, "qwe"] 13 Copy U
This article is about incorporating functional programming concepts into Ruby code, in a pragmatic way – something I call “functional style.” And when I say pragmatic, what I mean is that the code should still mostly look and feel like idiomatic Ruby. Ruby is not Haskell, and nor should it be. The idea is to work with the grain of the language, not against it. The code should be easy for any Rubyi
The Enumerable module is one of my favorite parts of Ruby. It allows us to perform operations like #each, #map, #inject, and #any? on collection objects, most notably Array. It’s all much cleaner, readable, and semantic than the nested for loops I used to write in other languages. Enumerable is made possible by leveraging one of Ruby’s more powerful constructs: blocks. They typically take the form
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く