Multi-stage selection (Ruby Ractor)

Multi-stage selection Answer date series:Yield practice/Nesting generators/Implementation of integer square root and cube root
problem http://nabetani.sakura.ne.jp/hena/ord24eliseq/
https://qiita.com/Nabetani/items/1c83005a854d2c6cbb69
Ruby 2014/8/2(On the day) https://qiita.com/cielavenir/items/9f15e29b73ecf98968a5
C#/Python 2014/8/4 https://qiita.com/cielavenir/items/a1156e6a4f71ddbe5dcb
Drop from here_prev_square/drop_prev_Answer before putting together cubic
Go/C#/Ruby/Python 2014/8/5 https://qiita.com/cielavenir/items/2a685d3080862f2c2c47
PHP/JavaScript 2014/9/9 https://qiita.com/cielavenir/items/28d613ac3823afbf8407
VB 2014/9/10 https://qiita.com/cielavenir/items/cb7266abd30eadd71c04
D 2015/12/21 https://qiita.com/cielavenir/items/47c9e50ee60bef2847ec
Perl 2017/3/10 https://qiita.com/cielavenir/items/6dfbff749d833c0fd423
Lua 2017/3/13 https://qiita.com/cielavenir/items/c60fe7e8da73487ba062
C++20(TS) 2017/3/15 https://qiita.com/cielavenir/items/e1129ca185008f49cbab (MSVC)
https://qiita.com/cielavenir/items/1cfa90d73d11bb7dc3d4 (clang)
F# 2017/3/17 https://qiita.com/cielavenir/items/a698d6a26824ff53de81
Boo/Nemerle 2017/5/13 https://qiita.com/cielavenir/items/e2a783f0fe4b0fe0ed48
Perl6 2017/5/15 https://qiita.com/cielavenir/items/656ea17fa96c865c4498
Kotlin 2017/5/25 https://qiita.com/cielavenir/items/9c46ce8d9d12e51de285
Crystal 2018/5/8 https://qiita.com/cielavenir/items/1815bfa6a860fd1f90db
MoonScript 2018/6/16 https://qiita.com/cielavenir/items/8b03cce0386f4537b5ad
Julia/Rust 2018/12/20 https://qiita.com/cielavenir/items/3ddf72b06d625da0c4a5
Nim 2018/12/26 https://qiita.com/cielavenir/items/5728944867e609fd52a7
Tcl 2018/12/31 https://qiita.com/cielavenir/items/76cbd9c2022b48c9a2c9
Pascal/Cobra 2019/1/16 https://qiita.com/cielavenir/items/81b81baf8dfc1f877903
Icon 2019/1/17 https://qiita.com/cielavenir/items/889622dcc721f5a4da24
Swift 2020/5/31 https://qiita.com/cielavenir/items/3b0b84a218e35d538f7f
Java/Groovy/Scala 2020/5/31 https://qiita.com/cielavenir/items/7f058203a8fd03b65870
V 2020/10/17 https://qiita.com/cielavenir/items/df30a6c101a97a713df5
Zig/Zen 2020/10/17 https://qiita.com/cielavenir/items/9cced9e4a94dcd70df0f
Pike 2020/11/2 https://qiita.com/cielavenir/items/3a8248f41611302b34fd
Vala/Smalltalk 2020/11/29 https://qiita.com/cielavenir/items/085dabe593cd916af5e8
Objective-C 2020/11/30 https://qiita.com/cielavenir/items/a1736e38789a3dd5cc5a
Ruby(Ractor) 2021/1/2 https://qiita.com/cielavenir/items/f493c6d512b63cc571cc
(Regarding the implementation of icbrt)Lemma 2017/5/11 N even for integer division/(x*y)Is n/x/Equal to y(Proof of that)
https://qiita.com/cielavenir/items/21a6711afd6be8c18c55

Early New Year (laughs [^ 1])

――Proc is not shearable. .. Did you make a destructive change? --I no longer use Proc # []. . () Is syntactic sugar (https://qiita.com/tbpgr/items/71a2bac3ec53277387d8) for # call (which works for all objects with the # call method).

#!/usr/bin/env ruby
#http://qiita.com/Nabetani/items/1c83005a854d2c6cbb69
#http://nabetani.sakura.ne.jp/hena/ord24eliseq/

raise 'Ractor version uses Ruby 3.0+. Try non_ractor version for your Ruby version (%s).'%RUBY_VERSION if RUBY_VERSION < '3.0'

class Integer
	def isqrt
		return 0 if self<=0
		return 1 if self<4 # 1
		x,y=0,self
		while x!=y&&x+1!=y
			x,y=y,(self/y+y)/2
		end
		x
	end
	def icbrt
		return -(-self).icbrt if self<0
		return 0 if self==0
		return 1 if self<8 # 1,7
		x,y=0,self
		while x!=y&&x+1!=y
			x,y=y,(self/y/y+y*2)/3
		end
		x
	end
end

def generate
	Ractor.new{
		i=1
		loop{
			Ractor.yield i
			i+=1
		}
	}
end

def drop_prev(check,prev)
	Ractor.new(check,prev){|check,prev|
		a=nil
		b=prev.take
		loop{
			a,b=b,prev.take
			Ractor.yield a if !check.(b)
		}
	}
end
def drop_next(check,prev)
	Ractor.new(check,prev){|check,prev|
		first=true
		a=nil
		b=nil
		loop{
			a,b=b,prev.take
			(first=false;Ractor.yield b) if (first || !check.(a))
		}
	}
end
def drop_n(check,n,prev)
	Ractor.new(check,n,prev){|check,n,prev|
		i=0
		loop{
			i+=1
			a=prev.take
			Ractor.yield a if !check.(i,n)
		}
	}
end

is_sq = Ractor.make_shareable ->n{n.isqrt**2==n}
is_cb = Ractor.make_shareable ->n{n.icbrt**3==n}
is_multiple = Ractor.make_shareable ->i,n{i%n==0}
is_le = Ractor.make_shareable ->i,n{i<=n}

f={
	'S'=>Kernel.method(:drop_next).to_proc.curry.(is_sq),
	's'=>Kernel.method(:drop_prev).to_proc.curry.(is_sq),
	'C'=>Kernel.method(:drop_next).to_proc.curry.(is_cb),
	'c'=>Kernel.method(:drop_prev).to_proc.curry.(is_cb),
	'h'=>Kernel.method(:drop_n).to_proc.curry.(is_le,100),
}
(2..9).each{|e|f[e.to_s]=Kernel.method(:drop_n).to_proc.curry.(is_multiple,e)}

if $0==__FILE__
	STDOUT.sync=true
	while gets
		x = $_.chomp.chars.reduce(generate){|s,e|f[e].(s)}
		puts 10.times.map{x.take}*','
	end
end

[^ 1]: Do your treatise

Recommended Posts

Multi-stage selection (Ruby Ractor)
Multi-stage selection (Java / Groovy / Scala)
I don't understand Ruby 3 Ractor
I tried to solve the problem of "multi-stage selection" with Ruby
Make bubble sort and selection sort in Ruby