Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jun 17 07:40
    evhub closed #497
  • Jun 17 07:40
    evhub commented #497
  • Jun 17 07:32
    evhub labeled #497
  • Jun 08 19:34
    tbsexton commented #581
  • Jun 08 18:31
    evhub labeled #581
  • Jun 08 18:31
    evhub closed #581
  • Jun 08 18:31
    evhub commented #581
  • Jun 08 14:01
    tbsexton commented #581
  • Jun 08 04:01
    evhub demilestoned #581
  • Jun 08 04:01
    evhub commented #581
  • Jun 08 01:39
    aananko commented #583
  • Jun 07 18:42
    evhub closed #583
  • Jun 07 18:40
    evhub labeled #583
  • Jun 07 17:37
    evhub commented #583
  • Jun 07 17:37
    evhub commented #583
  • Jun 07 17:37
    evhub milestoned #583
  • Jun 07 17:37
    evhub labeled #583
  • Jun 07 15:28
    aananko edited #583
  • Jun 07 14:17
    aananko opened #583
  • Jun 06 08:02
    aananko commented #582
Evan Hubinger
@evhub

@michaeltingley There's currently no way to do this using the Coconut Jupyter kernel, but you can do it with the Coconut Jupyter magic if you do %load_ext coconut and then:

%%coconut --mypy
x: str = 1

which should give you an error. Also, I should note that calling coconut.icoconut directly is generally not what you want to be doing; you should probably default to coconut --jupyter console or coconut --jupyter notebook instead.

Michael Tingley
@michaeltingley
This is very cool! Unfortunately, I can't get it to work. I'm getting the error variable names cannot start with reserved prefix _coconut. Here's a screenshot. Let me know if you have any ideas!

(There are also a few other warnings, which you can see in that image:

CoconutSyntaxWarning: unnecessary from __future__ import (Coconut does these automatically) (line 3)
  from __future__ import print_function, absolute_import, unicode_literals, division
CoconutSyntaxError: variable names cannot start with reserved prefix _coconut (line 4)
  import sys as _coconut_sys
                ^
  File "<string>", line unknown
SyntaxError: variable names cannot start with reserved prefix _coconut
  import sys as _coconut_sys

)

Evan Hubinger
@evhub
@michaeltingley Ah, the problem is you're using %%coconut from inside a Coconut kernel, which doesn't work since Coconut ends up trying to compile that code twice, once by %%coconut and once by the kernel itself. You should try using %%coconut inside of a Python kernel instead.
Michael Tingley
@michaeltingley
@evhub Oh, OK, thanks. So there's no way to get mypy enforcement inside a Coconut kernel I guess? Thanks anyways for the help :)
Evan Hubinger
@evhub
@michaeltingley Not currently, but there's no fundamental technical obstacle to it. Feel free to raise an issue for it.
Actually, one other thing you could do is just use something like mypy-ipython, which should work inside of a Coconut kernel, since Coconut's kernel supports magics/extensions (though I haven't tested mypy-ipython myself).
Mihai Eugen Miuta
@miutamihai
Hi everyone! I just started out using coconut and I absolutely love it (especially the lambda function syntax change). Speaking of lambdas, I was wondering whether there's a way of using the addpattern syntax with lambda expressions. That would absolutely seal the deal for me :smile:
Evan Hubinger
@evhub

@miutamihai Statement lambdas certainly offer support for pattern-matching syntax—for example:

xs |> map$(def ({"a": a}) -> a + 1)

And in terms of using addpattern, it's just a function, so you can certainly do something like:

def ignore_zero(x, 0) = float("nan")
safediv = addpattern(ignore_zero)(def (x, y) -> x / y)
Thurston Sexton
@tbsexton
@evhub how would you like #513 to be addressed in practice? IMO we typically see something that looks like coconut-language-server as its own separate repo. Do you want to make a base repo with the dev-ops stuff you prefer, for us to PR to it? I've been researching how to implement an LSP with pylgs: https://github.com/openlawlibrary/pygls
Thurston Sexton
@tbsexton
(unrelated: think I could do a quick PR to add this to your conf.py? the sidebar never scrolls unless I get waaaay down in the docs... just a minor pet peeve ^_^ https://github.com/ryan-roemer/sphinx-bootstrap-theme/issues/136#issuecomment-186860368)
Evan Hubinger
@evhub
@tbsexton Separate repo seems good and if you want to just make it that seems good to me. I don't anticipate being able to contribute a ton, but certainly if there's anything I can do on the Coconut side to make things easier I'd be happy to.
Re doc sidebar scrolling, I just pushed evhub/coconut@2bd81da which bumps the version per the PR you linked; hopefully that fixes the issue.
Thurston Sexton
@tbsexton
oh cool. You may need to add something like 'navbar_fixed_top': "false", to the html settings in sphinx, per the last comment on that issue
Evan Hubinger
@evhub
@tbsexton Ah, thanks—pushed evhub/coconut@78bb495
Thurston Sexton
@tbsexton
Hmm that didn't do what I expected ^_^ now the sidebar is locked to the top of the page (which i guess is obvious in retrospect haha) ... so we can't navigate from within the docs somewhere
Maybe the best "alternative" is the globaltoc_depth:3 option? It that does what I'm assuming, then the sidebar would just have less "stuff" in it.
Apologies, i'm not super familiar with the options in this sphinx theme (more of a mkdocs-material user myself)
Evan Hubinger
@evhub
Just reading the documentation, I don't think globaltoc_depth does what you want—it looks like it conrols the "Site" dropdown, not the navbar.
Andrey Logunov
@capissimo
A code snippet resource is essential for this project, imho, like the one for F# (http://www.fssnip.net/). Or is there any already?
Evan Hubinger
@evhub
@capissimo That does seem useful; closest to that currently would just be the tutorial, examples in the documentation, my repositories using Coconut, or just searching for Coconut code on GitHub.
Thurston Sexton
@tbsexton
@evhub fyi searching on Github leads to a bunch of results from the "Cocaine" language x)
On another note, this might be a bug: string matching inside dicts?
{'q'+n:val} = {'q1':2}
print(n)
CoconutParseError: parsing failed (line 1)
  {'q'+n:val} = {'q1':2}

  File "<string>", line unknown
SyntaxError: parsing failed
  {'q'+n:val} = {'q1':2}
the equivalend in a list works:
['q'+n, val] = ['q1',2]
print(n)
>>> 1
but not in a set
works in a tuple
Thurston Sexton
@tbsexton
Seems to only be a problem when the compound pattern is in the key, since it works when I try string-matching in the value
Actually, pattern-matching on the keys seems broken further, eg. using tuple destructuring in the key:
{('q1',n): '1'+val} = {('q1',2):'12'}
print(n)
Thurston Sexton
@tbsexton

yknow what, I literally asked this in 2019 haha. You said:

@tbsexton Dictionary keys in patterns currently have to be constants, unfortunately--there's no current syntax for looking up keys based on values, only for looking up values based on keys. Feel free to raise an issue for that, though.

so that answers it. I can look for/add an issue
Evan Hubinger
@evhub
Yup, that's a limitation of the pattern-matching system—and unfortunately it's a very tricky one to solve, since it leads to a combinatorial explosion of possible matches based on trying to figure out which key should match.
Evan Hubinger
@evhub
(It's the same reason you can't do head + [2, 3] + last = [1, 2, 3, 4].)
Thurston Sexton
@tbsexton

yeah that's a great point. Though I will say, my workaround was to use tuples which work fine.

It was a bit of an edge-case, perhaps. This was data formed from an OpenAPI schema, so they had a lists of single-pair dicts. [{k1,v1},{k2:v2},...] etc. So I was pattern matching on only one key to dispatch to whatever function needed to be executed on the value for a given key. I knew the dicts were singletons, so it was ok to use match def f(d is dict) = f(d.items()|>list|>.[0]) and do all the logic in tuples


I do have another question: what would the equivalent of yield from be in terms of maps?

When using a nested lazy sequence, even calling list on the resulting generator usually returns a list of a bunch of lazy sequences (of possibly unknown depth). Is there a command/pattern for recursively consuming/yielding all generators? I know in the past I used for gen in super-gen: yield from gen, but alas, this inhibits my quest to be rid of for-loops ^_^

Thurston Sexton
@tbsexton
(p.s. this is a super common pattern in e.g. Pandas dataframe construction, where there might be a single generator you pass to the constructor that yields rows, but internally you want it to be defined with sub-generators. Obviously I'd prefer to build these with map/fmap as opposed to for ... yield from but i always seem to end up with a top-level list full of un-executed generator functions
Thurston Sexton
@tbsexton
I did figure out a single-level version of this... top_gen |> (::) <*.. map$(nested_gen)
but this is not recursive... cannot exhaust all generators in the "stack".
Mostly just don't know what I'd call this, I'm imagining it's like a "flush" where every generator gets exhausted if it exists, returning their result (e.g. into a list).
Evan Hubinger
@evhub
@tbsexton Maybe try:
def eval_iters(() :: it) =
    it |> map$(eval_iters) |> list

addpattern def eval_iters(x) = x
Thurston Sexton
@tbsexton
@evhub huh actually when you write it out like that, this is just an iterator over a "tree" of iterators to grab all the leaves in order.
like a DFS where we just return the leaves
we may not need the |> list part, rather leaving as a generator. the whole "stack" of generators would get flattened into one generator over the leaves
Evan Hubinger
@evhub
No, I don't think so—the function as I wrote it shouldn't flatten anything, and without list I don't think it does anything.
Evan Hubinger
@evhub
Here's a more general function that might help showcase what eval_iters is doing:
def recursive_map(func, () :: it) =
    it |> map$(recursive_map$(func)) |> func

addpattern def recursive_map(func, x) = func(x)

def list_it(() :: it) = list(it)
addpattern def list_it(x) = x

eval_iters = recursive_map$(list_it)

(|1, (|2, 3|), 4, (|5, 6|)|) |> eval_iters |> print
Henning
@henningsway
I use VScode (Insiders) with the new native Notebook API for interactive Python Scripting. As a former R user the functional possibilities of coconut appeal to me, but I have trouble selecting a coconut-kernel in vscode so far. It does work great for Jupyter in the Browser, though. Is there a chance to get this to work in vscode as well? :)
Evan Hubinger
@evhub
@henningsway Theoretically, VSCode supports TextMate syntax highlighting, which already exists for Coconut here—though I don't know how to get VSCode to use that file (I don't use VSCode myself). If you figure it out, I'd appreciate it if you could create a package or at least put the instructions here and I'll add them to the documentation.
kobarity
@kobarity
@evhub, I just published a VSCode extension kobarity.coconut based on your Coconut.tmLanguage. Thanks for your great work.
Actually, another extension based on the older version of Coconut.tmLanguage already exists, but it seems not maintained.
My new extension supports syntax highlighting on Coconut files and in Coconut code block in Markdown.
kobarity
@kobarity
vscode-insiders-coconut-jupyter.png
@henningsway, I tried VSCode Insiders and could select Coconut kernel. First I installed the extensions Jupyter, Python and Coconut. Then I connected to a remote Jupyter server, and selected the Coconut kernel.
Evan Hubinger
@evhub
Thanks @kobarity! I'll add that to the documentation.