Require & Imports of Modules: (Fuzzy Search from JS to Imba)

Can anyone convert this to imba syntax? I’m getting too many errors. Maybe someone can help me do it well. I’m not sure how to get around the outer part.
Here’s my attempt: https://scrimba.com/c/cD7QnBuv

'use strict';

function fuzzysearch (needle, haystack) {
  var tlen = haystack.length;
  var qlen = needle.length;
  if (qlen > tlen) {
    return false;
  }
  if (qlen === tlen) {
    return needle === haystack;
  }
  outer: for (var i = 0, j = 0; i < qlen; i++) {
    var nch = needle.charCodeAt(i);
    while (j < tlen) {
      if (haystack.charCodeAt(j++) === nch) {
        continue outer;
      }
    }
    return false;
  }
  return true;
}

module.exports = fuzzysearch;

What problem are you trying to solve? If it’s not for learning purpose then you could just use the fuzzysearch package since require is now working.

I’m more interested in learning imba syntax with these challenges, than in the actual end result. It’s cool that require is working though. i have yet to experience successfully requiring js modules.

I see, but it would be better IMHO if you are actually solving real problems you have.

Not knowing how to code something Imba syntax, is a real problem for me. :sweat_smile:
Someone helped me out thought, and it works great. I can’t remember who.

For future readers, here’s the working code:

export def fuzzyFeather feather, haystack
	let haystackLength = haystack.length
	let featherLength = feather.length
	if featherLength > haystackLength
		return false
	if featherLength is haystackLength
		return feather is haystack
	
	let featherLetter = 0
	while featherLetter < featherLength
		let haystackLetter = 0
		let match = false
		var featherLetterCode = feather.charCodeAt(featherLetter++)
		
		while haystackLetter < haystackLength
			if haystack.charCodeAt(haystackLetter++) is featherLetterCode
				break match = true
		continue if match
		return false
	return true

Hey Eric! I love the drive to translate algorithms designed in Javascript into imba, I Love how readable your imba code is (and not just because you used readablevariable names) and I share your passion for avoiding javascript in my imba projects. I am not sure if you are still looking for feedback but I think we could take advantage of some patterns that I like to use in ruby and really showcase imba’s advantages! This code is (as far as I could tell) equivalent to yours:
EDIT: strange behaviour from the base class that it will return false for scrambled strings that are the same length egg geg but will return true for scrambled strings where the second is longer geg, eggplant

export def fuzzyFeatherTwo(feather, haystack)
	return false if feather.length > haystack.length	
	return feather is haystack if feather.length == haystack.length # note we can remove both gaurd clauses and still pass all the tests
	
	for featherLetter in feather
		let match = for haystackLetter in haystack when haystackLetter is featherLetter 
				break true
		continue if match.length
		return false
	true

Here we user the String.length method inline to avoid having to declare local variables, take advantage of the fact that we can iterate over the characters in the sting using the for Char in String pattern and, take advantage of imba’s implicit return! Had to edit my response a lot because my initial response was missing a lot of the optimizations present in your code

I made a sandbox with both our methods and tests (written in imba) to prove that both solutions are correct if anyone wants to play around with it more! found here: https://scrimba.com/c/c7Pk6Lhd

I see you are working on the scrimba playground. I will take a look when I wake up. I appreciate this. I’m relatively new to web app development, so I welcome any tips and best practices to make my code better.

While I do work in a web-app development setting, I am pretty new to it as well, so definitely take my suggestions with a grain of salt! if you do have any questions, suggestions, or just find my code hard to read let me know. I would love to talk about it

@jmlancas You should report the bugs you noted on the scrimba playground. @somebee might be able to do something about that.

Great job on that refactoring of the code. It’s like half the lines of code. Amazing. I will drop it in a project I’m using my search on, and see how it works.

I’m gonna drop your code here incase something ever happens to the scrimba playground.

# jmlancas' refactor of fuzzySearch with imba
export def fuzzyFeatherTwo(feather, haystack)
	return false if feather.length > haystack.length	
	return feather is haystack if feather.length == haystack.length # note we can remove both gaurd clauses and still pass all the tests
	
	for featherLetter in feather
		let match = for haystackLetter in haystack when haystackLetter is featherLetter 
			break true
		continue if match.length
		return false
	true	

scrimba playground

You mention that require is working, but I get require is not defined in the console running from the imba/template.
@scanf

Which version of imba and imba-rollup are you using?

I’m on imba “2.0.0-alpha.34” and imba-rollup “1.0.4”
Oh and why is there an imba-rollup version matching the imba versioning.
There’s up to imba-rollup “2.0.0-alpha.31” I see. Is that more updated that 1.0.4?

@scanf
Require works on scrimba, but not on the imba/template
https://scrimba.com/c/cKK3aEcB

# WORKS on Imba/template
import {featherSearch} from './featherSearch'
import {ClassName} from './classes'

# NOT WORKING on Imba/Template
let fuzzy = require 'fuzzysearch' # node_module requires not working
require './featherSearch' # local requires not working either```
require './classes' # local requires not working either

I see, now I am following. So on Scrimba things behave a little bit different,
i.e. require is being transformed. What you can do instead in your example

is use import and that should work as expected.

Using your example it would like:


import fuzzy from ‘fuzzysearch'

console.log('fuzzy', fuzzy("he","hello"))

Does it make sense?

Maybe we should drop require support from Imba code all together? The idea being

everyone should use import and it looking consistent. I don’t think it will be affect

the JavaScript integration. What do you think @somebee?

Yes it’s newer. The versioning is just a tooling issue. We can’t override the
regular imba so @pre has been used which represents the 2.0.0-alph.x versions

and to have the same versioning scheme we are also using @pre for the rollup

package now.

The tool in use tracks changes so it won’t bump the version if

nothing changed https://lerna.js.org