lispとrubyとpythonと その3 リスト処理 (ruby)
ずいぶん空いた。
・まず12時前にはかえれない。
・週1〜3日は朝の4時とかまで働く。
・土日も結構仕事(家でできるけど)
って状態はどうなんでしょ??
つかもーイヤになってきた。
愚痴はともかく。
その3はリスト処理がメイン。
やることが多いのでこれだけで一回分にした。
↓に書いたRubyのサンプルはだいぶ前に書いたからもう内容あんまり覚えてない・・・。
#! /usr/bin/ruby -Ku #lambda式の追記 #1.9なのでこうかける f0 = ->a,b{puts a + b} #呼び出しはこんな感じ #昔と同じ f0.call("a","b") f0["a","b"] #こう書けるようになった #しかし微妙 f0.("a","b") #rubyのlambdaとProcの違いってなんなんだろ?? #ブロックをとる関数 def f_with_block(a,&body) #受け取るとProcになってる puts body body.("f_with_block:" + a) end f_with_block "test" do |s| puts s end #配列 arr = ["abc","def","ghi","jkl"] #取得 puts arr puts arr[0] puts arr[1] #回す arr.each do |a| puts a end arr.each_with_index do |a,i| puts i.to_s + ":" + a end #配列のメソッドはここ #http://doc.loveruby.net/refm/api/view/class/Array #気になったものだけサンプルを書く arr1 = [1,2,3] arr2 = [2,3,4] #積集合 puts (arr1 & arr2).to_s #和集合 puts (arr1 | arr2).to_s #差集合 puts (arr1 - arr2).to_s #choice #配列の要素をランダムに選ぶメソッド puts arr1.choice() #map(collect) puts arr1.to_s arr1.map do |e| e + 1 end puts arr1.to_s puts "------------------------------" #combination #これ便利かも arr1 = [1,2,3,4] arr1.combination 2 do |e| puts e.to_s end puts "------------------------------" #permutation arr1 = [1,2,3,4] arr1.permutation 2 do |e| puts e.to_s end #cycle arr1 = [1,2,3] puts arr1.zip(["a","b"].cycle).to_a.to_s #すぐに評価されちゃうから使えない #元の配列をクリアすれば止まる arr1 = [1,2,3] i = 0 arr1.cycle do |e| puts e arr1.clear if i > 10 i = i + 1 end #flatten #これもイイなー arr1 = [1,2,[3,[4,5],6],7,[8,9 puts arr1.flatten.to_s #pack...あとで調べる #product arr1 = [1,2,3] puts arr1.product(["a","b","c"],[:a,:b,:c]).to_s #transpose #行列入れ替え #これもいい。 arr1 = 1,2,3,4], ["data1","data2","data3","data4" puts arr1.transpose.to_s #uniq #重複の削除 arr1 = [1,2,2,3,4,5,3,1] puts arr1.uniq.to_s puts "------------------------------" #zip arr1 = [1,2,3] puts arr1.zip(["a","b","c","d"],[:a,:b]).to_a.to_s #Enumerable #all? arr1 = [2,4,6] puts arr1.all?{|e| e % 2 == 0 } #any? arr1 = [1,2,3,5] puts arr1.any?{|e| e % 2 == 0 } #count arr1 = [1,2,3,4,5] #countするとEnumerable::Enumeratorが戻って来てしまう #よくわからん puts "count:" + arr1.count.to_s puts "size:" + arr1.size.to_s #これならとれてる #arr1の中の2の要素数を表示 puts "count:" + arr1.count(2).to_s #arr1で偶数の要素数を表示 puts "count:" + arr1.count{|e| e % 2 == 0}.to_s #cycle arr1 = [1,2,3] puts arr1.zip(["a","b"].cycle).to_a.to_s #find detect #最初に条件を満たしたものをかえす arr1 = [1,2,3,4,5] puts arr1.find{|e| e > 4}.to_s #drop_while arr1 = [1,2,3,4,5] puts arr1.drop_while{|e| e < 3}.to_s #each_cons enum_cons arr1 = [1,2,3,4,5] arr1.each_cons 3 {|e| puts e.to_s} #each_slice enum_slice arr1 = [1,2,3,4,5] arr1.each_slice 2 {|e| puts e.to_s} #find_all select arr1 = [1,2,3,4,5] puts arr1.find_all{|e|e % 2 ==0}.to_s #find_index arr1 = [1,2,3,4,5] puts arr1.find_index{|e|e % 2 ==0} #first arr1 = [1,2,3,4,5] puts arr1.first.to_s puts arr1.first(2).to_s #grep arr1 = ["aaa","bbb","ccc"] puts arr1.grep(/^[bc]*$/).to_s #group_by tbl = "taro","python"], ["taro","ruby"], ["jiro","commonlisp"], ["jiro","pyhon"], ["jiro","sheme"], ["hanako","c"], ["hanako","ruby"], ["youko","python"], ["youko","commonlisp" puts tbl.group_by{|e|e[0]}.to_s #member? include? arr1 = [1,2,3,4,5] puts arr1.member?(2).to_s #inject reduce arr1 = [1,2,3] puts arr1.reduce([]) {|a,e| a + [e * 2] }.to_s #max arr1 = [1,2,3] puts arr1.max #max_by arr1 = [1,2,3] puts arr1.max_by{|e| e * -1 }.to_s #min arr1 = [1,2,3] puts arr1.min #min_by arr1 = [1,2,3] puts arr1.min_by{|e| e * -1 }.to_s #minmax arr1 = [1,2,3] puts arr1.minmax.to_s #none? arr1 = [1,3,5] puts arr1.none?{|e| e % 2 == 0} #one? arr1 = [1,2,3,5] puts arr1.one?{|e| e % 2 == 0} #partition arr1 = [1,2,3,4,5] puts arr1.partition{|e| e % 2 == 0}.to_s #reject arr1 = [1,2,3,4,5] puts arr1.reject{|e|e % 2 ==0}.to_s #sort arr1 = [2,3,1,4,5] puts arr1.sort.to_s #sort_by #take arr1 = [1,2,3] puts arr1.cycle.take(10).to_s #take_while arr1 = [1,2,3] puts arr1.cycle.each_with_index.take_while{|e| e[1] > 10} #zip arr1 = ["a","b","c","d"] puts arr1.zip([1,2].cycle,[:a,:b,:c].cycle).to_a.to_s #arrayが返ったり、Enumerable::Enumerator1が返ったり。 #どういうとき何がかえるのか正直分かってない arr1 = [1,2,3] puts arr1.cycle.take(10).class.name puts arr1.zip([1,2].cycle,[:a,:b,:c].cycle).class.name