Code Monkey home page Code Monkey logo

lazy_enum's Introduction

Convert to lazy any ruby enumerable.


Example 1: Return the square of the first 100 prime numbers greater than 1000

require 'mathn'
Prime.lazy.select {|x| x > 1000 }.map {|x| x**2 }.take(100)


Example 2: Find the 10001st prime. (Euler Proyect #7)

Prime.lazy.drop(10000).first


Example 3: Get the pytagorean triple which sum's equal to 1000. (Euler Proyect #9)

dove_tailing = Enumerator.new do |yielder|
  Math::Naturals.each do |z|
    0.step(z) do |y|
      0.step(z - y) do |x|
        yielder << [x, y, z - y - x]
      end
    end
  end
end

dove_tailing.lazy.select {|x,y,z| (z > y && y > x) && z**2 == x**2 + y**2 }.
  select {|x,y,z| x + y + z == 1000 }.first


Example 4: Calculate the sum of all the primes below two million. (Euler Proyect #10)

Prime.lazy.take_while {|n| n < 2_000_000 }.inject(:+)

lazy_enum's People

Contributors

pabloh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.