Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 08:35
    som-snytt commented #9635
  • 08:30
    som-snytt commented #10604
  • 05:54
    SethTisue commented #1515
  • 05:39
    SethTisue milestoned #802
  • 05:39
    SethTisue milestoned #470
  • 05:39
    SethTisue demilestoned #470
  • 05:38
    SethTisue assigned #802
  • 05:38
    SethTisue opened #802
  • 05:03
    SethTisue commented #1516
  • 05:02
    SethTisue commented #1516
  • 04:41
    SethTisue commented #1516
  • 04:40
    SethTisue converted_to_draft #1516
  • 04:40
    SethTisue commented #1516
  • 04:38
    SethTisue commented #1516
  • 04:35
    SethTisue commented #1516
  • 04:06
    SethTisue commented #1516
  • 03:33
    SethTisue commented #1516
  • 03:22
    SethTisue edited #1515
  • 03:21
    SethTisue edited #1516
  • 03:20
    SethTisue edited #1515
Ghost
@ghost~5fac9159d73408ce4ff3d745
I have wanted string.r.explain for an instrumented regex that tells me what it did and why.
Rob Norris
@tpolecat
It could give you a URL to rexper populated with the regex. I wish regexper provided a way to quote your regex because that ends up being a big source of bugs.
gitleet
@gitleet
When reading a inline function (anonymous lambda?), do you read right from left or left to right? f: (String, T) => String = (a: String, b: T) => b.toString
Rob Norris
@tpolecat
What do you mean?
gitleet
@gitleet
I have trouble reading that ....
function that takes 2 params, string and T, and returns a String?
Rob Norris
@tpolecat
Yeah I would say something like "String T to String"
The parameter types are inferred on the RHS so you can say ... = (a, b) => b.toString which shortens it a bit.
gitleet
@gitleet
how can I add this inline function to a value? val f = .... ?
Are inline function parameters in a def optional?
zeroexcuses
@zeroexcuses
Is https://www.artima.com/shop/programming_in_scala_5ed still "draft", or is the book done:? (I can not find this book at any other website, and I have never purchased from this website, and this website's design does not look very modern.)
Martijn
@martijnhoekstra:matrix.org
[m]
What do you mean with adding to a funtion in this context @gitleet ? If you have some val f: Int => String, you can pass it to a method just by its name. Say, you have a val l = List(1, 2, 3), then you can pass it to its map method with l.map(f)
or do you maybe want to compose to functions, where you have a val g: Double => Int and also a val f: Int => String, and you want a function that first does g and then f, you can compose that as g.andThen(f)
(or as f.compose(g), which has the downside that you'll look more mathy and the upside that everyone finds it harder to read)
(bonus points on insisting it's way more natural for you)
gitleet
@gitleet
I tried to do: val f = (String, T) => String = (a: String, b: T) => b.toString
Martijn
@martijnhoekstra:matrix.org
[m]
you're going wrong with the syntax. The geneal syntax when defining a value is val name: Type = value
name here is f, the type is (String, T) => String, and the value is (a: String, b: T) => b.toString
that gives you val f: (String, T) => String = (a: String, b: T) => b.toString
there's a few things to notice here
if you already have a fully expected type, you can leave the ascriptions of the parameters off:
//instead of 
val f: (String, T) => String = (a: String, b: T) => b.toString
//you can do
val f: (String, T) => String = (a, b) => b.toString //because you already know the expected type, you don't need to repeat it
I get a not found type T error
Martijn
@martijnhoekstra:matrix.org
[m]
another thing to note is that you're not using a at all! that's probably a bug. This is a toy example, but be mindful
if T is intended to be a type parameter, in scala 2, you can't do that, and in scala 3, there is special syntax for that
gitleet
@gitleet
this is scala2
Martijn
@martijnhoekstra:matrix.org
[m]
in scala 2, unfortunately you can't have a function with a type parameter
gitleet
@gitleet
as a value...so it works inline ok
Martijn
@martijnhoekstra:matrix.org
[m]
there are workarounds, but they're not very pretty. Best for now to forget about it
there isn't really any difference between inline and not inline
in your situation, it probably works because T is defined when you call it inline, and not if you don't
e.g. given def foo[T](f: T => Int), you can do foo((t: T) => 7) because T is defined there
It's good to note that you can't really do anything with it, precisely because it's abstract: in my example I ignore it entirely
in your example, you call toString on it, which you can always do
but that's about it for the things you can do with T of a type you don't know
it could just as well have been Any, you can't do anything more with it
gitleet
@gitleet
i see ok
thanks for your help
Martijn
@martijnhoekstra:matrix.org
[m]
glad to help
feel free to keep asking about whatever you run into
Luis Miguel Mejía Suárez
@BalmungSan
Note that if you need a generic function, you can use a method instead. Thanks to eta-expansion the method will be converted to a function when you use it; and in that moment it will pick a concrete type for the generic (like it was inlined).
So you can do this
def foo[T](t: T): T = t

List(1, 2, 3).map(foo)
gitleet
@gitleet
if I have a method that takes a function as a parameter...how do I make it optional?
I'm looking at some code that takes a function as an arguement...and in some cases this function is called w/o passing in the function paramter...
so parameters that are functions are optional?
Martijn
@martijnhoekstra:matrix.org
[m]
No, they're not. Maybe there is another method with the same name that doesn't take the function argument
Rob Norris
@tpolecat
It can have a default argument, maybe that's what's happening.
@ def foo(f: Int => Int = _ + 1): Int = f(42) 
defined function foo

@ foo() 
res1: Int = 43

@ foo(_ * 2) 
res2: Int = 84
gitleet
@gitleet
I searched for multiple def defintions..but only 1. I see no default argument either. Has to be something I am missing :)