Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Dec 06 06:48
    evhub demilestoned #519
  • Dec 06 03:15
    evhub milestoned #519
  • Dec 06 03:15
    evhub labeled #519
  • Dec 06 01:34
    bj0 opened #519
  • Dec 03 23:10
    evhub labeled #518
  • Dec 01 11:56
    QuestofIranon commented #514
  • Dec 01 07:37
    zimzion opened #518
  • Nov 26 00:07
    evhub closed #517
  • Nov 26 00:07
    evhub labeled #517
  • Nov 26 00:07
    evhub milestoned #517
  • Nov 26 00:07
    evhub labeled #517
  • Nov 26 00:07
    evhub labeled #517
  • Nov 26 00:07
    evhub opened #517
  • Nov 25 06:07
    evhub commented #496
  • Nov 24 23:31
    evhub commented #496
  • Nov 24 22:58
    evhub commented #514
  • Nov 24 22:34
    evhub closed #496
  • Nov 24 22:34
    evhub commented #496
  • Nov 24 22:29
    evhub reopened #496
  • Nov 24 22:28
    evhub closed #496
Sune Debel
@suned
is there a way to achieve something like this or is that something that would need to be contributed? :D
Sune Debel
@suned
I'm guessing that the reason is that _coconut_NamedTuple doesn't play nice with mypy in this case
Evan Hubinger
@evhub
@suned _coconut_NamedTuple is just typing.NamedTuple, so that shouldn't be the problem. maybe raise an issue for this?
Sune Debel
@suned
@evhub excellent, will do!
Matthew Huie
@mphuie
Are there any simple examples of |*> and |**> ? Trying to figure out how to pipe keyword arguments
For something like this cw.describe_alarms(AlarmNames=[], MaxRecords=100)
pavelbraginskiy
@pavelbraginskiy
@mphuie {'AlarmNames': [], 'MaxRecords': 100} |**> cw.describe_slarms
Err, alarms.
Evan Hubinger
@evhub
Alternatively, I like dict(AlarmNames=[], MaxRecords=100) |**> cw.describe_alarms for dictionaries you plan on using as keyword arguments.
aloisdg
@aloisdegouvello_gitlab
@evhub As a F# dev, playing with Python in the side. Coconut is awesome. Thank you :)
Evan Hubinger
@evhub
@aloisdegouvello_gitlab Glad to hear it! And feel free to raise an issue if you have any suggestions.
aloisdg
@aloisdegouvello_gitlab
@evhub I will! I also created the first Coconut question on StackOverflow: https://stackoverflow.com/questions/57758993/how-to-use-currying (I missed the $ and I didnt know that partial application was another name for currying.)
Nicholas A. Del Grosso
@nickdelgrosso

Hi! Can anyone show how to make a data type that has a limited set of values (e.g. a positive-only integer)? I see how to do it with algebraic data types:

data PosInt(val is int if val > 0)

However, this seems more like a composite, and I'd like to be able to do arithmetic with these values without knowing the names of the attributes. Is there a nice way to do this? I'm new to coconut and functional programming in general, and apologize if this has already been answered or is an anti-pattern--any pointers would be very helpful!

Evan Hubinger
@evhub
@nickdelgrosso How about:
data PosInt(x is int if x > 0):
     def __add__(PosInt(x), PosInt(y)) = PosInt(x + y)
     def __sub__(PosInt(x), PosInt(y)) = PosInt(x - y)
     def __mul__(PosInt(x), PosInt(y)) = PosInt(x * y)
     def __div__(PosInt(x), PosInt(y)) = PosInt(x / y)
Nicholas A. Del Grosso
@nickdelgrosso
Thank you @evhub for the suggestion! That's a bit more extensive than I was expecting, but passing the data through the constructor again makes sense.
davidabrahams
@davidabrahams
curious how you would recommend foreaching a list in coconut? ie, I don't want the return value, I want to perform a side-effect with each element in the list.
anything better then:
for x in L:
    x.effect()
Thurston Sexton
@tbsexton
@davidabrahams something like a map+lambda? Like
L = ['hi', 'HI', 'Hi', 'hI']

low = L |> map$(x-> x.lower()) |> list
low |> print
>>> ['hi', 'hi', 'hi']
Of course, generally in FP it's my understanding we are trying to avoid side effects (meaning, you set things up so that the return value is the thing you want; see my example where the list output is returned)
Thurston Sexton
@tbsexton
but using anonymous functions it's generally possible to access attributes of the collection items. in specific cases, you can use implicit partials to do the same thing, cleanly
aloisdg
@aloisdegouvello_gitlab
@tbsexton +1
Nicholas A. Del Grosso
@nickdelgrosso
Is there a Coconut equivalent to the typing package's NewType function?
Something that essentially transpiles to "Name = NewType('Name', str)"?
Evan Hubinger
@evhub

@nickdelgrosso Coconut supports a bunch of additional syntax on top of the typing library, but fundamentally Coconut isn't an alternative to typing but a supplement--everything just gets compiled to use typing so you're welcome to include the use of any typing stuff in your code. One thing I would recommend doing, though is using TYPE_CHECKING to do something like

if TYPE_CHECKING:
    from typing import NewType
Name = NewType("Name", str)

so that you don't require typing at runtime (which is especially nice if you want to support Python 2).

Nicholas A. Del Grosso
@nickdelgrosso
@evhub thanks for your reply! I find the NewType function to be worry, though. If Coconut supported a more concise version, it would be very helpful (like "Name: str", which currently treats Name as an instance of string rather than a subclass).
Nicholas A. Del Grosso
@nickdelgrosso
*wordy
Actually, it's not true--Name: str actually becomes a None in coconut, while in Python it is "not defined".
Evan Hubinger
@evhub
@nickdelgrosso Name: str sets Name to None on --target universal since Python < 3.6 doesn't support the Name: str syntax. If you pass --target 3.6, on the other hand, then Name will stay undefined.
pavelbraginskiy
@pavelbraginskiy
@evhub Installing coconut[all] doesn't work
ERROR: Could not find a version that satisfies the requirement trollius>=2.2; extra == "all" (from coconut-develop[all]) (from versions: 2.1.post2)
ERROR: No matching distribution found for trollius>=2.2; extra == "all" (from coconut-develop[all])
Evan Hubinger
@evhub
@pavelbraginskiy Should be resolved with coconut-develop as of #515.
Thurston Sexton
@tbsexton

I think I misunderstand some of the match statement finer points. if I have some id like idx='a' defined, plus a list of tuples that have (idx,val) pairs, I cant use matching to return the value of the idx I want, right? i.e. an in statement is not part of the match syntax?

because

match (idx, val) in tuple_list:
    print(val)

is not working...I guess I would have to map a sub-match over all of the tuples? one that matches on (idx, val) and returns nothing if no match occurred?

Thurston Sexton
@tbsexton

if I was going to be cheeky about this problem, I would probably say

(tuple_list
 |>  dict
 |> .get(idx, default)
)

which would work by merit of key-value lookup being fast. is there a pattern-matching solution to matching a substructure within an iterable that doesn't rely on class promotion shennaigans?

Evan Hubinger
@evhub
@tbsexton If you want to do lookup in a list of tuples by only the first element of the tuple then you really should just convert it into a dictionary. You can still use match if you want, though—just do:
match {=idx: val, **_} in dict(tuple_list):
    print(val)
Thurston Sexton
@tbsexton
@evhub such an elegant syntax! I somehow glossed over the spot in the docs where the =<var> pattern syntax was mentioned. Actually since it can be so powerful (and some of us are learning to pattern-match in Coconut coming from Python) some more examples of basic pattern match workflows/use-cases would be pretty nice in the docs (the long list of possible syntax options is great but can be a bit hard to process while problem-solving).
e.g. I just saw this example of how F#'s pattern matching made for elegant fizz-buzz solution http://trelford.com/blog/post/FizzBuzz.aspx ...these kinds of "practical" examples might be super nice for coconut. almost like mini/advanced tutorial series for after the vector making one.
I'm definitely trying to keep track of simple elegant examples I come across while using so I can submit some eventually.
Thurston Sexton
@tbsexton

A couple follow up questions I ran into using my data structures....

  • the above works because dicts are technically un-sorted, so {=idx:val, **_} doesn't imply anything about the position of the idx key in the dict. what if I'm searching in an ordered set? e.g. searching for a sub-list within a list where that sublist starts with 0? Do I have to know where that sublist lies to make a valid pattern? The docs say the [] pattern keeps track of the size of the list explicitly as part of the match.

  • I ran into a problem where I had a list of functions (produced parametrically) and wanted to get back a list of results by mapping a value to all of the functions. See here for Haskell version: https://stackoverflow.com/questions/27080626/haskell-apply-single-value-to-a-list-of-functions

Thurston Sexton
@tbsexton
but I don't think the $ operator functions as an applicator here as it does in haskell. [(+)$(2), (*)$(2), (**)$(2)] |> map$(($)(4)) gives a typeerror, saying the first argument must be callable.
any shorthand/operator for x,f-> f$(x)? Or just "apply function" operator?
Evan Hubinger
@evhub
@tbsexton The tutorial has some match examples, but I agree that more in the docs would be nice. Definitely submit a PR if you have some that you think would be good! For matching sorted data structures, yes, it will match the order. And in Coconut, the $ operator is partial application, not function application. The function application operator in Coconut is <| (see https://coconut.readthedocs.io/en/master/DOCS.html#pipeline).
Alamar
@AlamarW
Hey, I'm new to coconut and I'm running into a problem
code def data():
with open('data_1') as data:
return [x for x in data]
oof hold on
```def data():
with open('data_1') as data:
return [x for x in data]
def data():
    with open('data_1') as data:
        return [x for x in data]
Anyway, so I'm trying to read from a txt file 'data_1' and am printing it with data |> print
but my response is just the <function data at 0x....>
Alamar
@AlamarW
Nevermind, I found a solution to the problem. Thanks for the help though!