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.
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 ^_^
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
|> list
part, rather leaving as a generator. the whole "stack" of generators would get flattened into one generator over the leaves
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
--target 3
(or above) and also supports a host of unicode alternatives to built-in operators.
typeclasses
or something, though addpattern
is probably going to be better than any library like that if you're working in Coconut.
toolz
's valmap
function?
addpattern fmap(f, obj is MyClass) = obj.map(f)
fmap
be a pattern matched function so that this functionality could be added to third party libraries
fmap
withmatch def my_fmap(obj is ThirdPartyObject, func) = ...
addpattern def my_fmap(obj, func) = fmap(obj, func)
Hey all, I'm really new to coconut, and I think this is the right place for this question, but if I'm wrong, let me know.
Is there anyway in coconut to pipe an object and then call a method of that object, passing one of that objects' attributes as an argument of the method?
This question really arises from using pipes with pandas where I want to use a pipe to process a dataframe, and in that pipe change the index of that dataframe and then later access this new index as an argument within a .groupby() call or something similar. Of course I could assign a new dataframe variable, and complete this task but I was hoping there was a way to avoid it.
Here is a simple (and useless) example of what I'd like to do:
class A_Class:
attribute = 1
def method (self, argument):
print(f"The argument is {argument}")
an_object = A_Class()
an_object |> \
.method(.attribute)
Output: The argument is operator.attrgetter('attribute')
My Desired Output: The argument is 1
Is there anyway to do what I'm trying to do? Am I misunderstanding something crucial? Thank you!