GithubHelp home page GithubHelp logo

Comments (9)

ms-tg avatar ms-tg commented on May 15, 2024

In Scala, is this List#partition and List#headOption?

from functionaljava.

amarpotghan avatar amarpotghan commented on May 15, 2024

@ms-tg yup

from functionaljava.

knutwalker avatar knutwalker commented on May 15, 2024

safeHead or headOption is List#toOption

from functionaljava.

amarpotghan avatar amarpotghan commented on May 15, 2024

Ah, Sure. Thanks.

On Fri, Feb 27, 2015 at 10:21 PM, Paul Horn [email protected]
wrote:

safeHead or headOption is List#toOption


Reply to this email directly or view it on GitHub
#111 (comment)
.

from functionaljava.

tonymorris avatar tonymorris commented on May 15, 2024

+1

However, safeHead is better written as a Prism

https://hackage.haskell.org/package/lens-4.7/docs/Control-Lens-Prism.html

https://hackage.haskell.org/package/lens-4.7/docs/Control-Lens-Cons.html

Note that writing prisms, in general, in Java requires code repetition, due to lack of higher-kinds. The idea here, however, is that _head can perform a "safe head retrieval" as well as "safe update head" operation. Further to this, you can compose prisms, so you might say something like:

safeHead.compose(children).update(upperCase)

which gets the children of an object (e.g. Person => List<Person>), then gets the first child (safeHead) and updates that child by upper-case. The entire expression returns the outer object type.

from functionaljava.

julien-truffaut avatar julien-truffaut commented on May 15, 2024

@tonymorris safeHead is not a Prism as you cannot recreate the original List. However, if you compose cons: Prism[List[A], (A, List[A])] with _1: Lens[(A, B), A] you get safeHead. The result of the composition is an Optional (in monocle) or a Traversal (in haskell lens if I am not mistaken).

from functionaljava.

jbgi avatar jbgi commented on May 15, 2024

so, with #130, you would get:

  public static <A> Optional<List<A>, A> _head() {
    return optional(
        l -> l.toOption().toEither(nil()),
        a -> l -> l.list(nil(), h -> tail -> cons(a, tail))
        );
  }

from functionaljava.

tonymorris avatar tonymorris commented on May 15, 2024

@julien-truffaut Yes you are right.

from functionaljava.

mperry avatar mperry commented on May 15, 2024

Created #161 to add List#partition.

from functionaljava.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.