Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Stefan Bertels
    @StefanBertels
    Regarding ToString()there is an issue / discussion here: louthy/language-ext#593
    Joe Taber
    @infogulch
    My method doesn't cover infinite Seqs, though I dont' use that feature in my code right now
    Joe Taber
    @infogulch
    I have an IEnumerable<Seq<T>>, how can I join all of them into a Seq<T>?
    Joe Taber
    @infogulch
    I used .Fold(new Seq<T>(), (acc, s) => acc.Concat(s)) which is somewhat unsatisfying but works fine
    Ah, nope I wanted to wrap my IEnumerable with Seq() then use Flatten()
    Stefan Bertels
    @StefanBertels
    Yes. Alternatively you could use LINQ (bind) to flatten things of different IEnumerable types if you want: Seq(from inner in outer from x in inner select x)
    Joe Taber
    @infogulch
    I tried using the LINQ system, but it never really worked in a way that was useful for me. Maybe I just didn't understand it
    Joe Taber
    @infogulch
    I ended up doing a bunch of things like this:
        public Validation<Error, object> Parse(YamlNode doc, Context ctx)
            => doc.AcceptNodeType<YamlSequenceNode>()
                .Map(n => Seq(n.Children))
                .MapT(c => ItemType.Parse(c, ctx))
                .Bind(vs => vs.Sequence())
                .Map(Conversions.SeqConverter(ItemType.Type));
    Joe Taber
    @infogulch
    Can you get an item out of a Map without throwing? i.e. return an option or something?
    Joe Taber
    @infogulch
    Find(K) is what I wanted
    Joe Taber
    @infogulch
    Is there a way to map just the FAIL type of a Validation? I want to modify the failure without modifying the success
    Remi Guittaut
    @remiguittaut
    Hi. I have a simple (stupid) question. I come from Scala, so I'm pretty used to the concept and the reason of the bottom type (= Nothing in Scala), but I can't wrap my head around something, which I would do easily in Scala. I have an EitherAsync<Ko, Lst<smth>>. I just want to transform the right case into a left if the list is empty. The natural way to do that is .Filter( lst => !lst.Any() ), but then I get an Either in the bottom case. I don't get why it doesn't give me a Left case, given through a default value that I would give. And How to achieve that in an elegant way? There is no MapBottom, or IfBottom, etc. How do you guys solve that?
    Nevermind, I can obviously flatMap it (= Bind in language-ext), but it's a bit inelegant.
    Remi Guittaut
    @remiguittaut
    For anyone else having the same problem:
    .Bind(list => 
          (list.Any() ? Right<Ko, Lst<???>>(list) : Left<Ko, Lst<???>>(Ko())).ToAsync()
    );
    Paul Louth
    @louthy

    @remiguittaut Bottom in lang-ext isn't really a type, it's just a state that certain types can be in because of the way a struct can be created using default(StructType). So, if I did default(Either<string, object>) then there's no way for the Either to be in a valid state (because Either doesn't accept null as a valid value). And so if you want to provide a default left value you must be explicit.

    You could wrap your expression into an extension method:

            public static EitherAsync<L, Lst<R>> DefaultIfEmpty<L, R>(this EitherAsync<L, Lst<R>> ma, L defaultIfEmpty = default) =>
                isnull(defaultIfEmpty) 
                    ? throw new ArgumentNullException(nameof(defaultIfEmpty))
                    :  ma.Bind(list => list.Any()
                                           ? RightAsync<L, Lst<R>>(list)
                                           : LeftAsync<L, Lst<R>>(defaultIfEmpty));

    Which would allow you to do this when L is a struct:

        ma.DefaultIfEmpty();

    And this when it's not (or you want to provide a useful L):

        ma.DefaultIfEmpty(leftValue);
    @infogulch Use MapFail to map the FAIL part of the Validation
    You can also use BiMap to flip a success to a fail and fail to a success if necessary.
    Stefan Bertels
    @StefanBertels
    I started a PR some time ago regarding IfBottom/MapBottom. It's not merged because it's not easy to implement (at least in a generic way) and I got stuck: louthy/language-ext#502 If anyone wants to help with that...
    Stefan Bertels
    @StefanBertels

    Hi everyone, is there some composition/trait feature or is there a good replacement in C#?

    I often want to extend an existing class / add a new property like this:

    class AnotherProperty<T> : T  // or something like "use T"
    {
         public int Number => 3;
    }
    So that AnotherProperty<List<string>> would have all properties of List<string> plus Number.
    Is there a chance to build something like this using code generation (as you can see I get inspired by latest [Union] feature...)?
    Paul Louth
    @louthy

    @StefanBertels There's nothing currently like that in lang-ext. It sounds like a use-case for a [NewType] attribute. Usually, this would be done via composition rather than inheritance. Wrapper methods and properties could then be added to encapsulate the behaviour of the T type. It would need some smarts to deal with immutable types that return themselves, and instead return the new type.

    i.e.

        [NewType(typeof(Lst<string>))]
        public partial class AnotherProperty
        {
            public int Number => 3;
        }

    It could generate:

        public partial class AnotherProperty
        {
            readonly Lst<string> @value;
    
            public AnotherProperty(Lst<string> value) => this.@value = value;
    
            public AnotherProperty Add(string value) => new AnotherProperty(this.@value.Add(value));
        }

    Not a trivial amount of work, but I can see the use-case. I'm back in very busy land, so probably not going to happen quickly.

    Stefan Bertels
    @StefanBertels

    Ok, I see the issue with return values (and parameters) of inner type. And of course conflicts could arise. And probably all extensions methods will get lost. Hmm.

    edit: remaining comment deleted, it was misleading, maybe later...

    Rich Bryant
    @richbryant
    evening. I expect this probably gets asked a lot (regarding one or other objects) but is there an example anywhere of how to use State<S, A>?
    Stefan Bertels
    @StefanBertels
    @richbryant: Have a look at these links, they have info/examples for Reader/Writer
    louthy/language-ext#351
    louthy/language-ext#602
    Paul Louth
    @louthy
    @richbryant One of my other projects uses the State monad to manage errors and actions when an Actor fails: https://github.com/louthy/echo-process/blob/master/Echo.Process/Strategy/Strategy.cs
    Rich Bryant
    @richbryant
    thank you!
    Joe Tremblay
    @jltrem
    Having a brain lapse looking for an option usage pattern. I know that I've done this before but the solution isn't coming to mind.
    Given a function that returns an Option of that thing: I want that value if it exists, otherwise I want to try to generate it; i.e., IfNone(() => ) but the result of that may be Some or None.
    IfNone needs to return an actual thing ... not an option... and I don't recall how to work around this.
    Joe Tremblay
    @jltrem
    in the above situation, is there anything prettier than this:
    maybe.Match(
       Some: x => Some(x),
       None: () => FooToMaybeCreateX());
    Stefan Bertels
    @StefanBertels
    ||
    Greg Wintering
    @gwintering
    @jltrem - Are you thinking of the | operator? This will give you an Option with 1 as the value:
    None | Optional(1)
    Stefan Bertels
    @StefanBertels
    -verbose: You can just use the or operator (||) to build something like
    var result = optionA || optionB || optionC

    And you can use Try(...).ToOption() for a factory method that might fail (=> None).

    IIRC Option itself has a lazy mode (i.e. constructor with function and lazy evaluation),too.

    Stefan Bertels
    @StefanBertels

    Seems lazy option was removed some time ago (https://github.com/louthy/language-ext/blob/master/LanguageExt.Tests/OptionLazy.cs) -- You can build you're own or use Try.

    And as @gwintering wrote: you will get Option<T> because that's what you asked for. If you want a value of T you have to append IfNone with some value.

    Joe Tremblay
    @jltrem
    @StefanBertels @gwintering I was unaware of the || operator. Very nice! Thanks for your help.
    Joe Tremblay
    @jltrem
    started a git repo last week to put "remember how to" examples for language-ext as I run across things that weren't immediately obvious to me. Might eventually be useful to others. jltrem/language-ext-playground@904df30
    Stefan Bertels
    @StefanBertels
    Yes, documentation and examples are a good thing. Another option for this: adding tests to LanguageExt itself. Reading tests might be useful to understand things like this. There are tests for ||already: https://github.com/louthy/language-ext/blob/master/LanguageExt.Tests/OptionCoalesceTests.cs
    Joe Tremblay
    @jltrem
    Agreed, the tests have been a good help. But I've only gone into them when I was looking for something specific. Reading through them "just for fun" would help find out what is actually available. Thanks again for your help.
    Stefan Bertels
    @StefanBertels
    Same here :-) I find new (easier) syntax every so often. Mostly by looking in LanguageExt source code or by asking here. A good IDE with intellisense showing type information is a plus.
    Astimus
    @Astimus
    Helo. I have Arr<Option<A>>. I want to convert it to Arr<A>.(all which are in Some state) Is there any pretty solution for such case?
    Astimus
    @Astimus
    public static Arr<T> AllWhichAreSome<T>(
                this Arr<Option<T>> list) =>
                list.Fold(
                    Arr<T>.Empty,
                    (source, newItem) => newItem.Match(source.Add, () => source));
    My extension method
    Paul Louth
    @louthy
    @Astimus
    list.Somes().ToArr()
    Astimus
    @Astimus
    @louthy thank you for help
    Paul Louth
    @louthy
    @Astimus You may also find it useful to switch from using Arr<T> to Seq<T>. Seq<T> is also backed by an array, but is much more powerful and useful for manipulating the collection.
    Astimus
    @Astimus
    @louthy thank you for advice