Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TODO] add items(table) to allow using algorithms on a table #9217

Closed
wants to merge 1 commit into from

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Oct 5, 2018

without this, we can't use algorithms on tables and are forced to use explicit for loops

@skilchen
Copy link
Contributor

skilchen commented Oct 5, 2018

isn't this a perfect opportunity to use a template?

template items[K,V](t: Table[K, V]): tuple[key: K, val: V] =
     pairs(t)

@timotheecour
Copy link
Member Author

timotheecour commented Oct 5, 2018

template items*[K,V](t: Table[K, V]): tuple[key: K, val: V] =
  ## EDIT: some doc comment (without this, no error)
  pairs(t)

when isMainModule:
  let a = {10:"foo", 12:"bar"}.toTable
  for ai in a:
    echo ai

Error: ignored invalid for loop

EDIT seem above line marked EDIT

@skilchen
Copy link
Contributor

skilchen commented Oct 6, 2018

Thats not true for me. It only happens, if you add additional code to the items template.

import tables

template items*[K,V](t: Table[K, V]): tuple[key: K, val: V] =
  pairs(t)

when isMainModule:
  let a = {10:"foo", 12:"bar"}.toTable
  for ai in a:
    echo ai

==>

(key: 10, val: "foo")
(key: 12, val: "bar")

@timotheecour
Copy link
Member Author

timotheecour commented Oct 6, 2018

It only happens, if you add additional code to the items template.

indeed, it's hitting this bug #9230 which I just filed.
I can do another PR to change it to your suggested way after #9230 gets fixed; but otherwise it' no good because we can't doc-comment items

@Araq
Copy link
Member

Araq commented Oct 9, 2018

Seems silly to me.

without this, we can't use algorithms on tables and are forced to use explicit for loops

YES! Exactly! A for loop is one single construct to learn that supports ALL of these epicycles of functional programming with its never ending map, fold, foldr, sum, groupby...

@krux02
Copy link
Contributor

krux02 commented Nov 8, 2018

I don't disagree on an items on a table. But I doubt it should be an alias to pairs. items normally yields the values and pairs keys and values. Since it is unclear what the best option for items is I prefer to not provide an items. Then local implementations may vary without conflics in the name.

@Araq Araq closed this Dec 21, 2018
@timotheecour timotheecour changed the title add items(table) to allow using algorithms on a table [TODO] add items(table) to allow using algorithms on a table Dec 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants