Skip to content

Commit

Permalink
feat: 🍻 remove duplicated items using gen
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshihiro503 committed Jun 28, 2024
1 parent 81f7446 commit 9bf2a0e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 1 addition & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ let () =
Log.debug "book is:\n %s" (AddressBook.dump book);
Log.debug "rules is:\n %s" (List.map Rule.dump rules |> String.concat "\n ");
let scenarios =
List.map (fun rule -> Scenario.seq book rule) rules
|> List.map (fun seq -> Seq.take 10 seq |> List.of_seq)
List.map (fun rule -> Scenario.gen 10 book rule) rules
|> List.concat
in
scenarios |> List.iter (fun scenario -> Log.debug "SC: %s" (Scenario.dump scenario));
Expand Down
7 changes: 5 additions & 2 deletions bin/scenario.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ let gen_aux book rule : (string * string * string) Gen.t =
return (src_ip, Ip.to_string dst, "")
end

let gen book rule =
let gen n book rule =
let desc idx = !%"%s-%04d" rule.Rule.name (idx+1) in
gen_aux book rule
|> Gen.to_seq
|> Seq.mapi (fun idx (src_ip, dst_ip, url_domain) ->
|> Seq.take n
|> List.of_seq
|> list_uniques
|> List.mapi (fun idx (src_ip, dst_ip, url_domain) ->
{src_ip; dst_ip; url_domain; description=(desc idx)})

let seq_addr addr =
Expand Down
16 changes: 16 additions & 0 deletions lib/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ let rec list_shuffle rand xs =
| Some(x, xs) -> x :: list_shuffle rand xs
| None -> []

let rec elem x xs =
match xs with
| [] -> false
| y :: ys -> x = y || elem x ys

let list_uniques l =
let rec helper l acc =
match l with
| [] -> acc
| x :: xs ->
if elem x acc then
helper xs acc
else
helper xs (x :: acc)
in List.rev (helper l [])

module SeqMonad = struct
type 'a t = 'a Seq.t

Expand Down

0 comments on commit 9bf2a0e

Please sign in to comment.