0 Members and 2 Guests are viewing this topic.
class Junction attr_accessor :value, :coelements attr_reader :type ANY = "|";ALL = "&" def initialize(num_of_values,typeof,array=nil) if block_given? index = 0;@value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;@value = array else;@coelements = (@value = Array.new(num_of_values)).size.to_i end @coelements = @value.size.to_i @type = (typeof=="any" or typeof=="all")? ((typeof=="any")?ANY: ALL): ((typeof==ANY)?ANY: ALL) #always defaults to ALL if not valid end def forall if block_given? index = 0;while index < coelements yield @value.at(index), index;index+=1 end else;raise "No block given to \'forall\' method" end end def ==(value) if @type==ANY return @value.include?(value) elsif @type==ALL return (@value==value)?true:false else;raise "Unknown Junction Typing error in \'==\' method" end end def !=(value) if @type==ANY return !(@value.include?(value)) elsif @type==ALL return (@value==value)?false:true else;raise "Unknown Junction Typing error in \'==\' method" end end def <(value) return @value.max < value end def >(value) return @value.min > value end def <=(value) return @value.max <= value end def >=(value) return @value.min >= value end def ===(value) return (@value.include?(value) and @value.at(@value.index(value))===value) end def to_a#override return @value.to_a end def to_s#override return @value.to_s.gsub(", ",((@type==ANY)?ANY: ALL)) end def to_i @value.each {|i|(i = (i.to_i)) rescue i = 0}.to_a;return self end def to_f @value.each {|i|(i = (i.to_f)) rescue i = 0.0}.to_a;return self end def <<(value) @value.push(value);return self end def push_to_last(value) @value.push(value);return self end def pop_last! return @value.pop end def pop_last_array! @value.pop;return self end def type_change(new_type=nil) if new_type;@type = new_type else;@type = ((@type==ANY)?ALL: ANY) end end def elements? @coelements.to_i end def clean! @value.uniq!;return self end def pretty_puts #not needed, but cool for easy reading :3 self.forall {|i,j|puts "#{(j+1).to_s}" << format_number_th(j) << " possible value is #{i.to_s}" } end private#helpers def format_number_th(j)#helper for pretty_puts if [10,11,12].include?(j%100);return "th" else;return (["st","nd","rd","th"].at((j%10>=4)?3:j%10)).to_s end endend
superpos = Junction.new 5,Junction::ANY,[1,2,3,4,5]superpos.pretty_putsputs superpos.to_sputs superpos.to_i.to_sputs superpos.to_a.to_sputs (superpos << 6).to_sputs superpos.pop_last_array!.to_sputs superpos == 3 #allsuperpos.type_changeputs superpos == 3 #anyputs superpos.to_ssuperpos.forall{|value,coelement_i| puts "\\o/ hai mr.#{value} from #{coelement_i} I liek chocolat mikl"}
1st value is 12nd value is 23rd value is 34th value is 45th value is 5[1|2|3|4|5][1|2|3|4|5][1, 2, 3, 4, 5][1|2|3|4|5|6][1|2|3|4|5]falsetrue[1&2&3&4&5]\o/ hai mr.1 from 0 I liek chocolat mikl\o/ hai mr.2 from 1 I liek chocolat mikl\o/ hai mr.3 from 2 I liek chocolat mikl\o/ hai mr.4 from 3 I liek chocolat mikl\o/ hai mr.5 from 4 I liek chocolat mikl
class Junction attr_accessor :value, :coelements attr_reader :type ANY = "|";ALL = "&";ONE = "@";NON = "~";NIL = "?" def initialize(num_of_values,typeof,array=nil) if block_given? index = 0;@value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;@value = array else;@value = Array.new(num_of_values) end @coelements = @value.size.to_i @type = ((typeof==ANY or typeof==ALL)?((typeof==ANY)?ANY: ALL):(NIL)) @type = ((typeof==ONE or typeof==NON)?((typeof==ONE)?ONE: NON):(@type)) return self end def any(num_of_values,array=nil) a = Junction.new(num_of_values,Junction::ANY,array) if block_given? index = 0;a.value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;a.value = array else;a.value = Array.new(num_of_values) end a.coelements = a.value.size.to_i return a end def all(num_of_values,array=nil) a = Junction.new(num_of_values,Junction::ALL,array) if block_given? index = 0;a.value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;a.value = array else;a.value = Array.new(num_of_values) end a.coelements = a.value.size.to_i return a end def one(num_of_values,array=nil) a = Junction.new(num_of_values,Junction::ONE,array) if block_given? index = 0;a.value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;a.value = array else;a.value = Array.new(num_of_values) end a.coelements = a.value.size.to_i return a end def non(num_of_values,array=nil) a = Junction.new(num_of_values,Junction::NON,array) if block_given? index = 0;a.value = Array.new(num_of_values){|value| yield value, index;index+=1} elsif array;a.value = array else;a.value = Array.new(num_of_values) end a.coelements = a.value.size.to_i return a end def forall if block_given? index = 0;while index < coelements yield @value.at(index), index;index+=1 end else;raise "No block given to \'forall\' method" end end def ==(value) case @type when ANY return @value.include?(value) when ALL occurances=1;@value.each {|index| occurances+=((index==value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index==value)?1:0) } return occurances==1 when NON return (@value.include?(value))?false:true when NIL;return nil end end def !=(value) case @type when ANY return @value.include?(value)?false:true when ALL occurances=1;@value.each {|index| occurances+=((index!=value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index!=value)?1:0) } return occurances==1 when NON return (@value.include?(value)) when NIL;return nil end end def <(value) case @type when ANY return (@value.include?(value))?(@value.max < value):(nil) when ALL occurances=1;@value.each {|index| occurances+=((index < value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index < value)?1:0) } return occurances==1 when NON return (@value.include?(value))?((@value.max < value)?false:true):(nil) when NIL;return nil end end def >(value) case @type when ANY return (@value.include?(value))?(@value.max > value):(nil) when ALL occurances=1;@value.each {|index| occurances+=((index > value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index > value)?1:0) } return occurances==1 when NON return (@value.include?(value))?((@value.max > value)?false:true):(nil) when NIL;return nil end end def <=(value) case @type when ANY return (@value.include?(value))?(@value.max <= value):(nil) when ALL occurances=1;@value.each {|index| occurances+=((index <= value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index <= value)?1:0) } return occurances==1 when NON return (@value.include?(value))?((@value.max <= value)?false:true):(nil) when NIL;return nil end end def >=(value) case @type when ANY return (@value.include?(value))?(@value.max >= value):(nil) when ALL occurances=1;@value.each {|index| occurances+=((index >= value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index >= value)?1:0) return occurances==1 } when NON return (@value.include?(value))?((@value.max >= value)?false:true):(nil) when NIL;return nil end end def ===(value) case @type when ANY return (@value.include?(value) and @value.at(@value.index(value))===value) when ALL occurances=1;@value.each {|index| occurances+=((index===value)?1:0) } return occurances==@coelements when ONE occurances=0;@value.each {|index| occurances+=((index===value)?1:0) } return occurances==1 when NON return (@value.include?(value) and @value.at(@value.index(value))===value)?false:true when NIL;return nil end end def to_a#override return @value.to_a end def to_s#override character=NIL;case @type when ANY character=ANY when ALL character=ALL when ONE character=ONE when NON character=NON end return @value.to_s.gsub(", ",character) end def to_i @value.each {|i|(i = (i.to_i)) rescue i = 0}.to_a;return self end def to_f @value.each {|i|(i = (i.to_f)) rescue i = 0.0}.to_a;return self end def <<(value) @value.push(value);return self end def push_to_last!(value) @value.push(value);return self end def pop_last! return @value.pop end def pop_last_array! @value.pop;return self end def type_change(new_type) case new_type when ANY @type=ANY when ALL @type=ALL when ONE @type=ONE when NON @type=NON else @type=NIL end end def elements? @coelements.to_i end def clean! @value.uniq!;return self end def pretty_puts #not needed, but cool for easy reading :3 self.forall {|i,j|puts "#{(j+1).to_s}" << format_number_th(j) << " possible value is #{i.to_s}" } end private#helpers def format_number_th(j)#helper for pretty_puts if [10,11,12].include?(j%100);return "th" else;return (["st","nd","rd","th"].at((j%10>=4)?3:j%10)).to_s end endend
Chicken nuggets == goodjust seeing if anyone is awake, I have no replies and I feel sad :-/
Quote from: Ashbad on July 15, 2011, 09:40:02 pmChicken nuggets == goodjust seeing if anyone is awake, I have no replies and I feel sad :-/I'm awake. I'm sorry you feel sad. But I don't program Ruby, so I can't help you.
So could you do Junctions in TI Basic by comparing two lists? Or did I not understand the concept?
How is the value calculated then? I guess I don't understand Junctions.
Woah nice Ashbad! This reminds me when Tari (or was it Tanner? I don't really remember) made a Switch class for Python on Cemetech
Quote from: ephan on July 24, 2011, 05:35:06 amWoah nice Ashbad! This reminds me when Tari (or was it Tanner? I don't really remember) made a Switch class for Python on Cemetech Glad you like it yeah, I think tanner did the first one, and Kllrnohj came back the next day with an even more kick-ass one. Fortunately Ruby already has built-in capabilities for switches
Quote from: Ashbad on July 24, 2011, 09:08:02 amQuote from: ephan on July 24, 2011, 05:35:06 amWoah nice Ashbad! This reminds me when Tari (or was it Tanner? I don't really remember) made a Switch class for Python on Cemetech Glad you like it yeah, I think tanner did the first one, and Kllrnohj came back the next day with an even more kick-ass one. Fortunately Ruby already has built-in capabilities for switches I don't see the need for switches, but yeah it'd be good to have them default.