 ;but returns function which again accepts some state and will
 ;produce new value and next state. This yields flexibility which
 ;plain map lacks, flat-map is able to adjust behavior
-;based on value+;based on value
+
+;here - I try to mimic get, set and modify of FPINS/6.6
+(def get-state (fn [s] (make-state s s)))
+(def set-state (fn [s] (make-state nil s)))
+;(def modify-state (fn [s f]
+;                    (for [])))
+
+;here is step-machine function from Runar Bjarnasson's explanation of 6.1 on fpins google group
+;it is using core.match as writing with if, cond etc would make it ridiculously complex
+
+(defn machine [locked candies coins]
+         {:locked locked :candies candies :coins coins})
+
+(defn step-machine
+  "inputs is sequence of either coins or turns, s is 'machine' in focus"
+  [i s]
+  (match [i s]
+         [_ {:locked _ :candies 0 :coins _}] s
+         [:coin {:locked false :candies _ :coins _}] s
+         [:turn {:locked true :candies _ :coins _}] s
+         [:coin {:locked true :candies candies :coins coins}]
+         (machine false candies (inc coins))
+         [:turn {:locked false :candies candies :coins coins}]
+         (machine true (dec candies) coins)))
+

  (fpinscala.strictness-and-laziness/fold-right             ;Notice that this variant is using fold-right
    (unit-state '())                                              ;Check out that "unit-produced" function is used as z value (now - it wouldn't be really
    #(map2-via-flat-map-s % %2 (fn [a b] (cons a b)))                 ;correct to say it is "initial" but rather neutral (or terminating?) element
    fs))

(declare simple-rng-s)


        ;to make this working same way as in Scala/FPINS option
        nxt-rng (make-simple-rng-s nseed)
        n (.intValue (unsigned-bit-shift-right nseed 16))]
    (make-state n nxt-rng)))

(defn non-negative-int-fpins-s
  "FPINS variant of non-negative-int function, adapted to 'state' variant - haha - and I was thinking: Adding one is way too simple :)"
  [rng]
  (from-state (rng) :let [n nxt-rng]
            (if (< n 0)
              (make-state (- (inc n)) nxt-rng)
              (make-state n nxt-rng))))

;usage example
((state-sequence-fpins (repeat 5 non-negative-int-fpins-s)) (make-simple-rng-s 42))
