Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Pattern matching #5

Closed
arshajii opened this issue Jun 25, 2018 · 2 comments
Closed

Pattern matching #5

arshajii opened this issue Jun 25, 2018 · 2 comments
Projects

Comments

@arshajii
Copy link
Member

arshajii commented Jun 25, 2018

Typical pattern matching from e.g. OCaml or Rust should be fairly easy to implement.

More interesting is pattern matching for sequence types. Many sequence computations can potentially be expressed with this. For example, a simple hash function:

fun hash(s: Seq) -> Int:
  s match A t => 0 + 4*hash(t)
        | C t => 1 + 4*hash(t)
        | G t => 2 + 4*hash(t)
        | T t => 3 + 4*hash(t)
        | _   => 0
@arshajii arshajii added this to To do in alpha Jun 30, 2018
@arshajii arshajii moved this from To do to In progress in alpha Jul 4, 2018
@arshajii
Copy link
Member Author

arshajii commented Jul 11, 2018

This works now (see seq-matching).

Example code:

fun hash(s: Seq) -> Int {
  match s {
    case `A...`: return 0 + 4*hash(s[1:])
    case `C...`: return 1 + 4*hash(s[1:])
    case `G...`: return 2 + 4*hash(s[1:])
    case `T...`: return 3 + 4*hash(s[1:])
    case ``: return 0
  }
}

source args[0] as input {
  let s = input[:5]
  s |> print
  hash(s) |> print
}

Example output

GTGCA
110
TATAT
819
CCTGC
437
TCAAT
775

@arshajii
Copy link
Member Author

Another example:

fun has_N(s: Seq) -> Bool {
  match s {
    case ``: return false
    case `N...`: return true
    case `_...`: return has_n(s[1:])
  }
}

@arshajii arshajii moved this from In progress to Done in alpha Jul 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
alpha
  
Done
Development

No branches or pull requests

1 participant