Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 08:29
    scala-jenkins milestoned #9768
  • 08:06
    NthPortal edited #9768
  • 08:04
    NthPortal labeled #9768
  • 08:04
    NthPortal opened #9768
  • 08:04
    NthPortal labeled #9768
  • 07:46
    nwk37011 synchronize #9752
  • 07:12
    xuwei-k commented on cf1b35b
  • 06:17
    som-snytt commented #12464
  • 05:59
    NthPortal commented #9275
  • 05:45
    NthPortal commented #9388
  • 05:44
    NthPortal commented #12464
  • 05:38
    NthPortal synchronize #9388
  • 05:36
    NthPortal synchronize #9388
  • 05:36
    som-snytt commented #12464
  • 05:35
    NthPortal edited #9388
  • 05:34
    NthPortal edited #9388
  • 05:25
    NthPortal edited #9388
  • 05:18
    NthPortal commented #9388
  • 05:16
    NthPortal synchronize #9388
  • 05:11
    SethTisue commented #12464
Dan Plyukhin
@dplyukhin

Is it possible to define a "true" Scott-encoded datatype in Scala 3? Here's the encoding I want for lists:

type List[T] = [S] => (() => S, (T, List[T]) => S) => S
  // Error: Illegal cyclic type reference

I've seen people get around this using traits, but I'm wondering if it's possible to do with plain old function types. If it's not possible, I'd be interested in reading a paper that explains what the issue would be.

Seth Tisue
@SethTisue
@discretegames I'd be curious what the full stack trace looks like. you might try asking at https://users.scala-lang.org
@zeroexcuses http://minikanren.org/#implementations has a Scala section. (link offered as-is; I haven't looked at the Scala implementations myself)
Norman Liu
@dreamsmasher

@dplyukhin would wrapping List[T] as an opaque type work? type aliases are expanded, so recursive definitions will lead to non-termination. I get the same error in Haskell too:

ghci> type List t = forall s. (() -> s, (t, List t) -> s) -> s

<interactive>:3:1: error:
    Cycle in type synonym declarations:
      <interactive>:3:1-56: type List t =
                                forall s. (() -> s, (t, List t) -> s) -> s

but newtype List t = List ... works just fine

Norman Liu
@dreamsmasher
ah just ran it in the REPL, turns out you can't have recursive opaque types. An old-school value class works though:
class List[T](val runList: [S] => (() => S, (T, List[T]) => S) => S) extends AnyVal {}
zeroexcuses
@zeroexcuses
@SethTisue : I think minikanren although also logic programming, makes some non-trivial differences from prolog. Notably: prolog var lookup is O(1), minikanen: O(log n); prolog backtrack: O(n), minikanen: 'free' due to immutable data structures; then there is the lack of cut / 'impure' prolog, and most importantly, it is definitely hnot based on Warren Abstract Machine. I'm looking for something like the WarrenAbstractMachine, but in Scala. I want to see what 'classical prolog' could be like if augmented with all the JVM libraries.
Guillaume Martres
@smarter
@dplyukhin type aliases are really just aliases, they don't try to support recursive definition. The only kind of recursion we support is on the upper-bound alone like type Foo <: Bla[Foo] which has a long history (but is still a pain to support well given that it tends to interact with every other feature of the type system): https://dl.acm.org/doi/pdf/10.1145/99370.99392
also if you're into weird encodings of list there's https://github.com/lampepfl/dotty/blob/master/tests/run/boehm-berarducci.scala
sachins301
@sachins301
im starting out with scala and microservices. whats the difference between using play, finagle, http4s and vertx ? can someone point me towards the right direction.
Luis Miguel Mejía Suárez
@BalmungSan
@sachins301 they are all different projects with different philosophies.
Some of them are frameworks while other are libraries.
For micro services I think it would be better to remove play for the list and rather add akka.
After that, it comes down to how they approach the problem.
  • Akka uses the actor model, however, if you use akka-http and maybe Akka Streams then you don't need to care about it. This would be like a more OOP-like side of Scala; while still taking advantage of FP features like lambdas.
  • Not so sure about finagle, but it would probably be similar to Akka but without the actors and streams and rather more focus on Futures, some more Java-esque.
  • http4s would follow the "pure functional programming" paradigm (better described as Programs as Values). It plays well with other libraries of the typelevel ecosystem, like cats, cats-effect, fs2, skunk, etc.
  • There would also be ZIO as a different approach to pure FP.
  • Finally Vert.x is a Java framework with focuses on providing the event loop that made JS famous into the JVM.
sachins301
@sachins301
the services im trying to work on are basically for data processing and passing large json, in that aspect are there any parameters that I should specifically look into among these? Also it must be able to handle multiple requests at a time effectively with minimum downtime.
Luis Miguel Mejía Suárez
@BalmungSan
All should work, is more about how you prefer to write code.
sachins301
@sachins301
and i know finagle has an admin ui feature that we can use to monitor stats. I did see something on akka using crud, but felt like bit of work in it. any idea if http4s and vertx has something on those lines? @BalmungSan
Luis Miguel Mejía Suárez
@BalmungSan
I would guess Vert.x to have something similar.
For htpp4s no out of the box, you may need to integrate it with some tracing system.
sachins301
@sachins301
Thanks @BalmungSan . Will look deeper into them
aahoughton
@aahoughton:matrix.org
[m]
Morning. I'm new to scala, trying to figure out how to implement the equivalent of a finally block on a for comprehension. Code looks something like "for { f = OpenZipFile(); a = doSomething(f); b = doSomethingElse(f) } yield { ... }".
.. I need to close that zip file at the end of processing, prior to leaving the function. I could have a local variable, but that won't work if I'm dealing with Futures in that for, I think
Luis Miguel Mejía Suárez
@BalmungSan
@aahoughton:matrix.org I would suggest switching from Future to IO from cats-effect or ZIO from zio. And then using Resource / ZManaged
aahoughton
@aahoughton:matrix.org
[m]
I'll take a look, thanks. I'm locked into a variation on Future due to tooling, but if there are conversions I can jump through hoops.
Luis Miguel Mejía Suárez
@BalmungSan
@aahoughton:matrix.org yeah you may use IO there and then call unsafeToFuture() at the end.
To return a Future but take advantage of IO and Resource in your logic.
Seth Tisue
@SethTisue
as you can see this room is more active than IRC where I saw you asked the same question :-) the Scala Discord is even more active than Gitter these days, https://discord.gg/scala
and Mario's answer seems satisfactory
Dan Dutrow
@dutrow-qualytics
Can someone explain this one to me?
scala> val z = (1, "2", "3")
val z: (Int, String, String) = (1,2,3)

scala> val (i: Int, n : String, s: String) = z
val i: Int = 1
val n: String = 2
val s: String = 3

scala> val z = (1, "2", null: String)
val z: (Int, String, String) = (1,2,null)

scala> val (i: Int, n : String, s: String) = z
scala.MatchError: (1,2,null) (of class scala.Tuple3)
  ... 32 elided

scala> val (i, n, s) = z
val i: Int = 1
val n: String = 2
val s: String = null
Guillaume Martres
@smarter
the thing you're writing on the left hand side is a pattern
and patterns never match null
1 reply
Stephen Duncan Jr
@jrduncans
Is there a way to suppress the warning "match may not be exhaustive” for this line of code?: val List(lat, long) = line.split(',').toList
raymond95
@raymond95:minds.com
[m]
Hello
Martijn
@martijnhoekstra:matrix.org
[m]
@jrduncans can you supress it with @unchecked?
It can be tricky where it goes, if I has to guess, (line.split(',').toList : @unchecked)
Gajendra Naidu Thalapaneni
@GajendraNaidu
Need help, I have a list of database operations as Futures, when I run them sequentially, the DB update is not updated in the same sequence. Using AsyncExecutor, with Slick "3.3.3" and Postgresql.
Luis Miguel Mejía Suárez
@BalmungSan
@GajendraNaidu how are you defining and sequencing those operations?
Gajendra Naidu Thalapaneni
@GajendraNaidu
def sequentializeFutures[A, B](l: Iterable[A])(fn: A => Future[B])(implicit ec: ExecutionContext): Future[List[B]] =
    l.foldLeft(Future(List.empty[B])) { (previousFuture, next) =>
      for {
        previousResults <- previousFuture
        next <- fn(next)
      } yield previousResults :+ next
    }
Luis Miguel Mejía Suárez
@BalmungSan
That should ensure operations are executed in order.
Gajendra Naidu Thalapaneni
@GajendraNaidu
@BalmungSan But the spec fails once in a while, I have created and delete operations in a spec, looks like delete gets executed first.
Luis Miguel Mejía Suárez
@BalmungSan
Uhm, weird, that should work.
Maybe you can try with this to ensure it will use a single thread.
import scala.concurrent.{ExecutionContext, Future}
import java.util.concurrent.Executors

object FutureUtils {
  private implicit final val ec =
    ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())

  def sequentializeFutures[A, B](l: IterableOnce[A])(fn: A => Future[B]): Future[List[B]] =
    l.iterator.foldLeft(Future.succesful(List.empty[B])) {
      case (previousFuture, a) =>
        for {
          previousResults <- previousFuture
          next <- fn(a)
        } yield next :: previousResults
    }.map(_.reverse)
}
(I also did a couple of improvements)
If with this it still fails then it means the problem is in some of those futures.
Gajendra Naidu Thalapaneni
@GajendraNaidu
@BalmungSan I tried this too but does not work. I am checking in Slick channel.
Luis Miguel Mejía Suárez
@BalmungSan
¯\_(ツ)_/¯
This is why I prefer IO over Future
Gajendra Naidu Thalapaneni
@GajendraNaidu
Actually, I moved it Future from IO, because the team wants plain SCALA
Luis Miguel Mejía Suárez
@BalmungSan
Whatever floats your boat