#Code Review

1 messages · Page 1 of 1 (latest)

potent pollen
#

I found a simpler approach that passes all the same test cases. The key insight is including the prev value as an input to the helper function. My type signatures look like:

fn find_length_of_lcis(nums: List(Int)) -> Int

fn find_length_helper(nums, prev, streak, best) 
tough minnow
#

I'd also look into list.fold, since it looks like it should be a good fit what you're doing

glass sequoia
#

here's a very slight readability rewrite:

import gleam/int

pub fn find_length_of_lcis(nums: List(Int)) -> Int {
  case nums {
    [] -> 0
    [_] -> 1
    _ -> find_length_helper(nums, 1, 1)
  }
}

fn find_length_helper(
  nums: List(Int),
  max_length: Int,
  current_length: Int,
) -> Int {
  case nums {
    [] | [_] -> max_length
    [first, second, ..rest] if second > first -> {
      let new_current = current_length + 1
      let new_max = int.max(max_length, new_current)
      find_length_helper([second, ..rest], new_max, new_current)
    }

    [_, second, ..rest] -> find_length_helper([second, ..rest], max_length, 1)
  }
}
#

untested, of course

#

but lgtm

#

i flattened the case expression and took out a few {} where they weren't necessary

#

i recommend looking at actual changes like using list.fold too - i just was curious what i could do in terms of how pretty it was

icy orchid
#

As far as I see list.fold operates on each element of a list

#

But we need to loop through elements pairwise. Aka sliding window of 2

tough minnow
azure minnow
#

i think the recursive solution is much clearer than attempting to craft one from the list.* functions

rugged roost
#

Recursion nice