SodiumFRP / sodium

Sodium - Functional Reactive Programming (FRP) Library for multiple languages
http://sodium.nz/
Other
848 stars 138 forks source link

Return the Cell itself from CellLoop.Loop? #91

Closed ziriax closed 8 years ago

ziriax commented 8 years ago

I think it would be handy to let CellLoop.Loop return the Cell<T> itself instead of Unit.Default. Like that one can directly assign the return value to a readonly output cell property. Now it requires two statements, and that feels imperative to me.

A simple C# example I'm working on:

    public sealed class SimpleTextCircuit : ISimpleTextOutputs
    {
        public Cell<string> Text { get; }
        public Cell<bool> IsTextEmpty { get; }
        public Cell<bool> CanReverseText { get; }

        private SimpleTextCircuit(ISimpleTextInputs inputs)
        {
            var text = new CellLoop<string>();

            var userText = inputs.SetText;
            var clearText = inputs.ClearText.Map(_ => "");
            var reverseText = inputs.ReverseText.Snapshot(text, (_, t) => SimpleTextRules.Reversed(t));

            IsTextEmpty = text.Map(SimpleTextRules.IsTextEmpty);
            CanReverseText = text.Map(SimpleTextRules.CanReverse);
            // Very handy to Loop and assign in one go.
            Text = text.Loop(userText.OrElse(clearText).OrElse(reverseText).Hold("")); ;
        }

        public static ISimpleTextOutputs Create(ISimpleTextInputs inputs)
        {
            return new SimpleTextCircuit(inputs);
        }
    }

Also for StreamLoop this would be handy.

I don't think it would break any code?

Thanks, Peter

jam40jeff commented 8 years ago

In your example, why not just assign the CellLoop to the Text property in the first line of the constructor?

ziriax commented 8 years ago

Indeed! I will this silly issue.