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

Syntax for creating concrete HashMaps #726

Open
austinletson opened this issue Apr 10, 2024 · 2 comments
Open

Syntax for creating concrete HashMaps #726

austinletson opened this issue Apr 10, 2024 · 2 comments

Comments

@austinletson
Copy link
Contributor

In reviewing documentation examples for HashMap, @digama0 mentioned that it would be nice to have syntax for constructing concrete HashMaps.

The proposed syntax is {"one" ↦ 2, "two" ↦ 2} => HashMap.ofList [("one", 1), ("two", 2)].

@somombo
Copy link
Contributor

somombo commented May 10, 2024

I propose, #{"one" : 1, "two" : 2}

@Shreyas4991
Copy link
Contributor

@digama0 also gave the syntax invocations to accomplish this. Posted below for completeness:

import Std

def Std.HashMap.ofList' [BEq α] [Hashable α] (l : List (α × β)) : HashMap α β := .ofList l

open Lean
syntax kvPair := term ": " term
syntax "!{" (term ": " term),* "}" : term
macro_rules | `(!{$[$k : $v],*}) => `(Std.HashMap.ofList' [$[($k, $v)],*])

#eval !{1: 2, 3: 4}.contains 1

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

No branches or pull requests

3 participants