Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions argparse/help_render.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,22 @@ fn builtin_option_label(

///|
fn positional_entries(cmd : Command) -> Array[String] {
let display = Array::new(capacity=cmd.args.length())
for arg in positional_args(cmd.args) {
if arg.hidden {
continue
}
display.push((positional_display(arg), arg_doc(arg)))
}
format_entries(display)
format_entries(
[
for arg in positional_args(cmd.args) if !arg.hidden => {
(positional_display(arg), arg_doc(arg))
}
],
)
}

///|
fn subcommand_entries(cmd : Command) -> Array[String] {
let display = Array::new(capacity=cmd.subcommands.length() + 1)
for sub in cmd.subcommands {
if sub.hidden {
continue
let display = [
for sub in cmd.subcommands if !sub.hidden => {
(sub.name, sub.about.unwrap_or(""))
}
display.push((sub.name, sub.about.unwrap_or("")))
}
]
if help_subcommand_enabled(cmd) {
display.push(("help", "Print help for the subcommand(s)."))
}
Expand All @@ -267,7 +264,7 @@ fn subcommand_entries(cmd : Command) -> Array[String] {

///|
fn group_entries(cmd : Command) -> Array[String] {
let display = Array::new(capacity=cmd.groups.length())
let display = []
for group in cmd.groups {
let members = group_members(cmd, group)
if members == "" {
Expand Down Expand Up @@ -428,16 +425,11 @@ fn group_label(group : ArgGroup) -> String {

///|
fn group_members(cmd : Command, group : ArgGroup) -> String {
let members = []
for arg in cmd.args {
if arg.hidden {
continue
}
if arg_in_group(arg, group) {
members.push(group_member_display(arg))
[
for arg in cmd.args if !arg.hidden && arg_in_group(arg, group) => {
group_member_display(arg)
}
}
members.join(", ")
].join(", ")
}

///|
Expand Down
17 changes: 7 additions & 10 deletions argparse/parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,13 @@ fn group_usage_expr(
if group.name != name {
continue
}
let members = []
for member_name in group.args {
if args.search_by(arg => arg.name == member_name) is Some(idx) {
let item = args[idx]
if item.hidden {
continue
}
members.push(arg_usage_token_for_group(item))
}
}
let members = [
for member_name in group.args if args.search_by(arg => {
arg.name == member_name
})
is Some(idx) &&
!args[idx].hidden => arg_usage_token_for_group(args[idx])
]
if members.length() == 0 {
return None
}
Expand Down
5 changes: 1 addition & 4 deletions argparse/parser_lookup.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ fn resolve_help_target(

///|
fn split_long(arg : String) -> (String, String?) {
let parts = []
for part in arg.split("=") {
parts.push(part.to_string())
}
let parts = [ for part in arg.split("=") => part.to_string() ]
if parts.length() <= 1 {
let name = match parts[0].strip_prefix("--") {
Some(view) => view.to_string()
Expand Down
10 changes: 3 additions & 7 deletions argparse/parser_positionals.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@

///|
fn positional_args(args : Array[Arg]) -> Array[Arg] {
let ordered = []
for arg in args {
if arg.info is PositionalInfo(_) {
ordered.push(arg)
}
}
ordered
[
for arg in args if arg.info is PositionalInfo(_) => arg
]
}

///|
Expand Down
10 changes: 5 additions & 5 deletions bytes/internal/regex_engine/translate.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ fn translate(
Sequence(exprs) => (transl_seq(tc, exprs), pref)
Alternation(exprs) => {
// TODO: merge sequences
let alts = []
for expr in exprs {
let (cr, pref2) = translate(tc, expr)
alts.push(enforce_pref(ctx, pref, pref2, cr))
}
let alts = [
for expr in exprs if translate(tc, expr) is (cr, pref2) => {
enforce_pref(ctx, pref, pref2, cr)
}
]
(@automata.e_alt(ctx~, alts), pref)
}
Quantifier(q, expr) => {
Expand Down
6 changes: 1 addition & 5 deletions deque/deque.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1675,11 +1675,7 @@ pub fn Deque::join(self : Deque[String], separator : StringView) -> String {
/// ```
///
pub impl[A : ToJson] ToJson for Deque[A] with to_json(self : Deque[A]) -> Json {
let res = Array::make(self.length(), null)
for i, x in self {
res[i] = x.to_json()
}
Json::array(res)
Json::array([ for x in self => x.to_json() ])
}

///|
Expand Down
9 changes: 3 additions & 6 deletions env/env_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ fn get_cli_args_ffi() -> FixedArray[Bytes] = "$moonbit.get_cli_args"

///|
fn get_cli_args_internal() -> Array[String] {
let tmp = get_cli_args_ffi()
let res = Array::new(capacity=tmp.length())
for t in tmp {
res.push(utf8_bytes_to_mbt_string(t))
}
res
[
for t in get_cli_args_ffi() => utf8_bytes_to_mbt_string(t)
]
}

///|
Expand Down
22 changes: 8 additions & 14 deletions hashset/hashset.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,9 @@ pub fn[K] HashSet::iter(self : HashSet[K]) -> Iter[K] {
///|
/// Converts the hash set to an array.
pub fn[K] HashSet::to_array(self : HashSet[K]) -> Array[K] {
let arr = Array::new(capacity=self.size)
for entry in self.entries {
if entry is Some({ key, .. }) {
arr.push(key)
}
}
arr
[
for entry in self.entries if entry is Some({ key, .. }) => key
]
}

///|
Expand Down Expand Up @@ -672,13 +668,11 @@ pub fn[K] HashSet::copy(self : HashSet[K]) -> HashSet[K] {
///|
/// ToJson implementation for hashset
pub impl[X : ToJson] ToJson for HashSet[X] with to_json(self) {
let res = Array::new(capacity=self.size)
for entry in self.entries {
if entry is Some({ key, .. }) {
res.push(key.to_json())
}
}
Json::array(res)
Json::array(
[
for entry in self.entries if entry is Some({ key, .. }) => key.to_json()
],
)
}

///|
Expand Down
6 changes: 3 additions & 3 deletions immut/hashmap/HAMT.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,9 @@ pub fn[K : Eq + Hash, V] HashMap::from_array(
///|
/// Returns an array of all key-value pairs.
pub fn[K, V] HashMap::to_array(self : HashMap[K, V]) -> Array[(K, V)] {
let arr = Array::new(capacity=self.length())
self.each((k, v) => arr.push((k, v)))
arr
[
for k, v in self => (k, v)
]
}

///|
Expand Down
9 changes: 1 addition & 8 deletions immut/priority_queue/types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,5 @@ priv enum Node[A] {
pub impl[A : ToJson + Compare] ToJson for PriorityQueue[A] with to_json(
self : PriorityQueue[A],
) {
// note here iter requires Compare
// This requires some investigation
// CR: this syntax feels less intuitive
let output : Array[Json] = []
for item in self {
output.push(item.to_json())
}
Json::array(output)
Json::array([ for item in self => item.to_json() ])
}
6 changes: 3 additions & 3 deletions immut/sorted_map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ pub fn[K, V] SortedMap::filter_with_key(
///|
/// Returns an array of all key-value pairs in ascending key order.
pub fn[K, V] SortedMap::to_array(self : SortedMap[K, V]) -> Array[(K, V)] {
let arr = []
self.each((k, v) => arr.push((k, v)))
arr
[
for k, v in self => (k, v)
]
}

///|
Expand Down
6 changes: 1 addition & 5 deletions immut/sorted_set/immutable_set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -791,11 +791,7 @@ pub impl[A : Show] Show for SortedSet[A] with output(self, logger) {

///|
pub impl[A : ToJson] ToJson for SortedSet[A] with to_json(self) {
let capacity = self.iter().count()
guard capacity != 0 else { return Json::array([]) }
let jsons = Array::new(capacity~)
self.each(a => jsons.push(a.to_json()))
Json::array(jsons)
Json::array([ for a in self => a.to_json() ])
}

///|
Expand Down
6 changes: 1 addition & 5 deletions immut/vector/vector.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,7 @@ pub impl[A : Show] Show for Vector[A] with output(self, logger) {

///|
pub impl[A : ToJson] ToJson for Vector[A] with to_json(self) {
let len = self.length()
guard len != 0 else { return [] }
let res : Array[Json] = Array::make(len, null)
self.eachi((i, value) => res[i] = value.to_json())
Json::array(res)
Json::array([ for value in self => value.to_json() ])
}

///|
Expand Down
27 changes: 4 additions & 23 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ pub impl[A : Show] Show for List[A] with output(xs, logger) {
/// ToJson implementation for List.
/// Converts a list to a JSON array.
pub impl[A : ToJson] ToJson for List[A] with to_json(self) {
let capacity = self.length()
guard capacity != 0 else { return [] }
let jsons = Array::new(capacity~)
for a in self {
jsons.push(a.to_json())
}
Json::array(jsons)
Json::array([ for a in self => a.to_json() ])
}

///|
Expand Down Expand Up @@ -346,22 +340,9 @@ pub fn[A, B] List::rev_map(
/// }
/// ```
pub fn[A] List::to_array(self : List[A]) -> Array[A] {
match self {
Empty => []
More(x, tail=xs) => {
let arr = [x]
for cur = xs {
match cur {
Empty => break
More(x, tail=xs) => {
arr.push(x)
continue xs
}
}
}
arr
}
}
[
for x in self => x
]
}

///|
Expand Down
6 changes: 1 addition & 5 deletions priority_queue/priority_queue.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ pub fn[A : Compare] PriorityQueue::iter(self : PriorityQueue[A]) -> Iter[A] {

///|
pub impl[A : ToJson + Compare] ToJson for PriorityQueue[A] with to_json(self) {
let arr = []
for x in self {
arr.push(x.to_json())
}
Json::array(arr)
Json::array([ for x in self => x.to_json() ])
}

///|
Expand Down
6 changes: 1 addition & 5 deletions set/linked_hash_set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,7 @@ pub fn[K : Hash + Eq] Set::intersection(

///|
pub impl[X : ToJson] ToJson for Set[X] with to_json(self) {
let res = Array::new(capacity=self.size)
for v in self {
res.push(v.to_json())
}
Json::array(res)
Json::array([ for v in self => v.to_json() ])
}

///|
Expand Down
12 changes: 6 additions & 6 deletions sorted_map/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#deprecated("Use `keys_as_iter` instead. `keys` will return `Iter[K]` instead of `Array[K]` in the future.")
#coverage.skip
pub fn[K, V] SortedMap::keys(self : SortedMap[K, V]) -> Array[K] {
let keys = Array::new(capacity=self.size)
self.each((k, _v) => keys.push(k))
keys
[
for k, _ in self => k
]
}

///|
/// Return an iterator of values.
#deprecated("Use `values_as_iter` instead. `values` will return `Iter[V]` instead of `Array[V]` in the future.")
#coverage.skip
pub fn[K, V] SortedMap::values(self : SortedMap[K, V]) -> Array[V] {
let values = Array::new(capacity=self.size)
self.each((_k, v) => values.push(v))
values
[
for _, v in self => v
]
}
6 changes: 3 additions & 3 deletions sorted_map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ pub fn[K, V] SortedMap::values_as_iter(self : SortedMap[K, V]) -> Iter[V] {
///|
/// Returns an array of all key-value pairs in ascending key order.
pub fn[K, V] SortedMap::to_array(self : SortedMap[K, V]) -> Array[(K, V)] {
let arr = Array::new(capacity=self.size)
self.each((k, v) => arr.push((k, v)))
arr
[
for kv in self => kv
]
}

///|
Expand Down
Loading