It might.
I usually use discriminated unions to wrap possible function arguments. Below is what I was hoping for.
possible_args = ( option1: str, option2: int, option3: MyCustomDeeplyNestedObject )
def my_function( args : possible_args ) :
match args:
case str instance_variable: exprs / stmnts on strings
case int instance_variable: exprs / stmnts on ints
case MyCustomDeeplyNestedObject instance_variable: exprs / stmnts on MyCustomDeeplyNestedObject
But I'm not sure if I can use is
in that match statement. That would be checking to make sure the variable refers to the same object reference, right?
@vip-ehowlett is
in pattern-matching just does type-checking, not check that it's the same object reference. Here are some ways you could write your example in Coconut:
def my_function(x is str) = exprs/stmts on strings
addpattern def my_function(x is int) = exprs/stmts on ints
addpattern def my_function(x is MyCustomDeeplyNestedObject) = exprs/stmts on MyCustomDeeplyNestedObjects
or
def my_function(arg):
case arg:
match _ is str: exprs/stmts on strings
match _ is int: exprs/stmts on ints
match _ is MyCustomDeeplyNestedObject: exprs/stmts on MyCustomDeeplyNestedObjects
or
possible_args = (str, int, MyCustomDeeplyNestedObject)
def my_function(arg is possible_args):
case arg:
match _ is str: exprs/stmts on strings
match _ is int: exprs/stmts on ints
match _ is MyCustomDeeplyNestedObject: exprs/stmts on MyCustomDeeplyNestedObjects
If you're using coconut-develop
(or in the next released Coconut version), you'll also be able to do
def my_function(arg):
match arg:
case class str(): exprs/stmts on strings
case class int(): exprs/stmts on ints
case class MyCustomDeeplyNestedObject(): exprs/stmts on MyCustomDeeplyNestedObjects
if you prefer that syntax, which is making use of Coconut's implementation of PEP 622, which is fully supported in develop now and will come out in the next release.
@dontmindmehere When I use the website interpreter to run
nums = count()
num = next(filter(lambda i: i<3, nums), 0)
print(num)
num = nums |> filter$((i -> i<3), ?) |> next$(?, 0)
print(num)
I get
0
0
which seems right to me. What error are you seeing instead? Also, though this should work with the website interpreter, I think it is running a relatively outdated version of Coconut, so it won't have all the new features (though it should support everything you're using there).
num = nums
|> filter$((i -> i<3), ?)
|> next$(?, 0)
Hi - I was wondering if there's any way to reproduce the functionality of pip install -e
for a project with coconut files in it.
My guess is that you'd need to set up a file watcher (e.g., watchdog) to handle recompiling files when they're changed? But was wondering whether there might be alternative strategies
coconut --watch
—but better might be to use automatic compilation. If you make sure in the root of your project (e.g. the top-level __init__.coco
) you import coconut.convenience
then compile fully once, pip install -e
should do what you want. Also, if you're using coconut-develop
(which you can get via pip install -U coconut-develop
), you can run coconut --site-install
which will make coconut.convenience
imported automatically on Python start so you don't have to import it in the project root.
Just heard of this via the recent HN thread; started going through the interactive tutorial. The pattern matching "now you try it" page (https://hmcfabfive.github.io/coconut-tutorial/Patternmatch/Patternmatch_p3.html) is a little rough around the edges, imo. I get that its purpose is to teach pattern matching syntax, but the solution makes a lot of choices the motivations behind which I don't understand.
fwiw, my solution was
from collections import Counter
items |> Counter |> print
(which btw I was pretty tickled to hit on, coconut makes that pretty darn clean :) )
items
an iterator? Seems like it'd be fine to leave it as a list.for
loop instead of a while
loop and avoid manually defining a stop condition, manually calling next()
, etc.current
variable inside the match
branches? wouldn't an aluminums[0] += 1
work?countItems()
to accumulate results, which explains the single-element lists. One could use a single dict instead (arguably more Pythonic), but then of course you actually don't need the pattern matching at all because you just use the item string as the key and you only need to write a single case (which is how I hit on using Counter in the end).I was surprised enough at these choices that I was expecting another page saying things like "we prefer to use iterators in coconut because...", but that was the end of the pattern matching section. So I was considering thinking for a little bit to come up with a toy problem that really demonstrates how pattern matching can improve on vanilla python's approach - but I hesitated because I haven't gone through the whole tutorial yet and wanted to make sure I wasn't being totally oblivious to something.
Hey! I tried installing coconut through "pypy3 -mpip install coconut" and, despite the install looking like it went well, "coconut -h" isn't recognized as a command.
I'm an absolute beginner in both pypy3 and coconut, so feel free to correct me.
PATH
environment variable. See: https://datatofish.com/add-python-to-windows-path/
pypy3 -m coconut -h
.
Beginner here!
Looking forward to using Coconut but am stumped by something simple.
I'm trying to unpack my parameters (key, value) passed into the map function:
{"A": 1, "B": 2, "C": 3}.items() |*> map$((key, value) -> value) |> list |> print
This works but is "uglier":
{"A": 1, "B": 2, "C": 3}.items() |> map$(key_value -> key_value[1]) |> list |> print
[1, 2, 3]
@GianpieroCea Saw your question about data science.
I haven't seen any but I will say that, barring the lack of LSP that keeps me mostly using coconut for prototyping in the notebook, i use coconut constantly for data science. I love what it's done for the maintainability of my code, as well.
So with that, though I've avoided using coconut for codebases that require collaboration (since not many folks can write/read it right away), I would love to start a coconut-for-data-science tutorial.
Been learning/using jupyter book and that could be a great way to do something fast and pretty. let me know if you want to join a repo where we can plan out e.g. key contents and case studies, and I can help adding code based on my irl experience.
I'm trying to setup coconut inside Databricks and came across a bug when trying to use the coconut magics:
TypeError: run_cell() missing 1 required positional argument: 'raw_cell
`
However, looking at the backtrace it is clear that the positional argument was given ipython.run_cell(compiled, shell_futures=False)