From c733c05e9c4d2e049f777bed00ee686c7164ea48 Mon Sep 17 00:00:00 2001 From: ketmar Date: Tue, 23 Nov 2010 10:41:07 +0200 Subject: [PATCH] added some samples --- imgsrc/defs/base/numbers/rand.st | 5 --- samples/pass.st | 12 ++++++ samples/simul/simulation.st | 24 +++++++++++ samples/simul/store01.st | 48 ++++++++++++++++++++++ samples/simul/store02.st | 87 ++++++++++++++++++++++++++++++++++++++++ samples/testInject.st | 10 +++++ 6 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 samples/pass.st create mode 100644 samples/simul/simulation.st create mode 100644 samples/simul/store01.st create mode 100644 samples/simul/store02.st create mode 100644 samples/testInject.st diff --git a/imgsrc/defs/base/numbers/rand.st b/imgsrc/defs/base/numbers/rand.st index d402257..6067c0b 100644 --- a/imgsrc/defs/base/numbers/rand.st +++ b/imgsrc/defs/base/numbers/rand.st @@ -14,11 +14,6 @@ METHODS FOR Random ^obj ] -^between: low and: high [ - ^(Random new) between: low and: high. -] - - seed [ ^seed ] diff --git a/samples/pass.st b/samples/pass.st new file mode 100644 index 0000000..0d69d7c --- /dev/null +++ b/samples/pass.st @@ -0,0 +1,12 @@ +{ + | d i l s rand | + i := 20. + d := '0123456789abcdefghijklmnopqrstuvwxyz'. + l := d size. + s := String new: i. + rand := Random new. + 1 to: i do: [ :x | + s at: x put: (d at: (rand between: 1 and: l)) + ]. + s printNl. +} diff --git a/samples/simul/simulation.st b/samples/simul/simulation.st new file mode 100644 index 0000000..3c06974 --- /dev/null +++ b/samples/simul/simulation.st @@ -0,0 +1,24 @@ +class: Simulation [ + | currentTime nextEvent nextEventTime | + + ^new [ + | obj | + obj := self basicNew. + self in: obj var: #currentTime put: 0. + ^obj + ] + + time [ + ^currentTime + ] + + addEvent: event at: eventTime [ + nextEvent := event. + nextEventTime := eventTime. + ] + + proceed [ + currentTime := nextEventTime. + ^self processEvent: nextEvent + ] +] diff --git a/samples/simul/store01.st b/samples/simul/store01.st new file mode 100644 index 0000000..f680668 --- /dev/null +++ b/samples/simul/store01.st @@ -0,0 +1,48 @@ +Requires [ simulation ] + + +class: Customer [ + numberOfScoops [ + | num | + num := SmallInt atRandom % 3 + 1. + 'customer has ' print. num print. ' scoop' print. + num > 1 ifTrue: [ 's' print ]. + '.' printNl. + ^num + ] +] + + +Simulation subclass: IceCreamStore [ + | profit | + + ^new [ + | obj | + obj := super new. + self in: obj var: #profit put: 0.0. + obj sheduleArrival. + ^obj + ] + + sheduleArrival [ + self addEvent: Customer new at: (self time + (SmallInt atRandom % 5 + 1)) + ] + + reportProfits [ + 'profits are ' print. profit print. '.' printNl. + ] + + processEvent: event [ + 'customer received at ' print. self time print. '.' printNl. + profit := profit + (event numberOfScoops asFloat * 0.17). + self sheduleArrival + ] +] + + +{ + | store | + store := IceCreamStore new. + [ store time < 15 ] whileTrue: [ store proceed ]. + store reportProfits. +} diff --git a/samples/simul/store02.st b/samples/simul/store02.st new file mode 100644 index 0000000..dda213b --- /dev/null +++ b/samples/simul/store02.st @@ -0,0 +1,87 @@ +Requires [ simulation ] + + +class: DiscrepeProbability [ + | weights max | + + defineWeights: anArray [ + weights := anArray. + max := anArray inject: 0 into: [:i :e | i + e ]. + ] + + next [ + | index value | + value := SmallInt atRandom % max + 1. + index := 1. + [ value > (weights at: index) ] + whileTrue: [ + value := value - (weights at: index). + index := index + 1. + ]. + ^index + ] +] + + +class: Customer [ + | groupSize | + + ^new [ + | obj | + obj := self basicNew. + self in: obj var: #groupSize put: (SmallInt atRandom % 8) + 1. + ^obj + ] + + groupSize [ + ^groupSize + ] +] + + +Simulation subclass: IceCreamStore [ + | profit scoopDistribution | + + ^new [ + | obj | + obj := super new. + self; + in: obj var: #profit put: 0.0; + in: obj var: #scoopDistribution put: + ((DiscrepeProbability new); defineWeights: #(65 25 10)). + obj sheduleArrival. + ^obj + ] + + sheduleArrival [ + self addEvent: Customer new at: (self time + (SmallInt atRandom % 5 + 1)) + ] + + reportProfits [ + 'profits are ' print. profit print. '.' printNl. + ] + + processEvent: event [ + 'customer received at ' print. self time print. '.' printNl. + profit := profit + ((self scoopsFor: event groupSize) asFloat * 0.17). + self sheduleArrival + ] + + scoopsFor: group [ + | num | + num := 0. + group timesRepeat: [ num := num + scoopDistribution next ]. + 'group of ' print. group print. ' have ' print. num print. ' scoop' print. + num > 1 ifTrue: [ 's' print ]. + '.' printNl. + ^num + ] +] + + +{ + | store | + store := IceCreamStore new. + [ store time < 15 ] whileTrue: [ store proceed ]. + store reportProfits. +} diff --git a/samples/testInject.st b/samples/testInject.st new file mode 100644 index 0000000..3af6cb9 --- /dev/null +++ b/samples/testInject.st @@ -0,0 +1,10 @@ +{ + "simple inject:into: to sum elements in an array" + | array sum | + array := #( 1 2 3 4 5 ). + sum := array + inject: 0 + into: [ :inj :ele | inj + ele ]. + "answer: 15" + sum printNl. +} -- 2.11.4.GIT