How to use third party libraries with Scala REPL?

后端 未结 4 485
太阳男子
太阳男子 2020-12-02 21:38

I\'ve downloaded Algebird and I want to try out few things in the Scala interpreter using this library. How do I achieve this?

4条回答
  •  广开言路
    2020-12-02 22:35

    This is an answer using Ammonite (as opposed to the Scala REPL) - but it is such a great tool that it is worth mentioning.

    1. You can install it with a one liner such as:
    sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.1.2/2.13-2.1.2) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm
    

    or using brew on macOS:

    brew install ammonite-repl  
    

    For scala 2.10, you need to use an oder version 1.0.3:

    sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.0.3/2.10-1.0.3) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm
    
    1. Run Ammonite in your terminal:
    amm
    // Displays
    Loading...
    Welcome to the Ammonite Repl 2.1.0 (Scala 2.12.11 Java 1.8.0_242)
    
    1. Use in ivy import to import your 3rd part library:
    import $ivy.`com.twitter::algebird-core:0.2.0`
    

    Then you can use your library within the Ammonite-REPL:

    import com.twitter.algebird._
    import com.twitter.algebird.Operators._
    Map(1 -> Max(2)) + Map(1 -> Max(3)) + Map(2 -> Max(4))
    ...
    

提交回复
热议问题