项目作者: jusleg

项目描述 :
A slower way to generate for loops in Ruby with a funky java-like syntax
高级语言: Ruby
项目地址: git://github.com/jusleg/looop.git
创建时间: 2020-04-21T23:50:26Z
项目社区:https://github.com/jusleg/looop

开源协议:MIT License

下载


Looop ♻ Gem Version tests badge loop badge license badge

Gone are the days where you convert your entire project to Java just to get the nice for loop. You can now build similar looking loops that run much slower than your average loop.

Get started

gem install looop

require 'looop'

How to use Looop

Looop was built with simplicity as its core value. For this reason, we only offer one method: Looop.for.

Let’s say you are DJ Khaled, a typical Java developer, and you wish to craft a tweet with your signature catchphrase “Another one.” repeated 5 times

  1. # tweet.rb
  2. require 'looop'
  3. CATCHPHRASE = 'ANOTHER ONE.'
  4. tweet = ''
  5. Looop.for(index = 1, ->{index <= 5}, ->{index += 1}) do
  6. tweet.concat(CATCHPHRASE)
  7. end
  8. puts tweet
  9. #=> ANOTHER ONE.ANOTHER ONE.ANOTHER ONE.ANOTHER ONE.ANOTHER ONE.

I’m intrigued, what are these ->{} things?

I’m glad you asked, those are Procs. We’ll call these procs while running the loop. This is one of the main reasons that make looop a tad bit slower.

Do U even Big-O ?!1

I guess, you can run nested loops if that’s what you are asking.

  1. height = 5
  2. Looop.for(i = 1, ->{ i <= height }, -> {i += 1}) do
  3. Looop.for(h = height - i, -> {h > 0 }, -> {h -= 1}) do
  4. print " "
  5. end
  6. Looop.for(j = 1, -> {j <= i}, -> {j += 1}) do
  7. print "* "
  8. end
  9. print "\n"
  10. end

will print:

  1. *
  2. * *
  3. * * *
  4. * * * *
  5. * * * * *

Is this leetcode ready? Can I pass a technical interview with this?

Is this leetcode ready?

It depends, you might be able to solve the base case, but I’d be surprised if you didn’t time out during the evaluation of the secret cases.

Can I pass a technical interview with this?

Meh, as long as they don’t ask for it to be optimized for performance.

TL;DR: don’t.

Is it fast

no

  1. Looop.for(i=0, -> {i<1000}, -> {i+=1}) do
  2. Looop.for(j=0, -> {j<1000}, -> {j+=1}) do
  3. #noop
  4. end
  5. end

was 6 times slower than

  1. i = 0
  2. while i<1000 do
  3. j = 0
  4. while j<1000 do
  5. #noop
  6. j += 1
  7. end
  8. i += 1
  9. end

I don’t want to setup a small script to test it but I’m really interested to try it, can you make a repl.it?

Sure. @jusleg/looop">here.

Copyright (c) 2020, Famingo Inc.

This project is licensed under the MIT License.

Do you have a soundcloud I can follow?

No, but you can follow me on twitter. I’ll announce it there if I end up creating one.