Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:19:19+00:00 2026-06-14T08:19:19+00:00

I just started to learn Erlang, and really like their list comprehension syntax, for

  • 0

I just started to learn Erlang, and really like their list comprehension syntax, for example:

Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}].                          
FoggyPlaces = [X || {X, fog} <- Weather].

In this case, FoggyPlaces will evaluate to “london” and “boston”.

What’s the best way to do this in Ruby?

For example, an Array like (very common, I believe):

 weather = [{city: 'toronto', weather: :rain}, {city: 'montreal', weather: :storms}, {city: 'london', weather: :fog}, {city: 'paris', weather: :sun}, {city: 'boston', weather: :fog}, {city: 'vancounver', weather: :snow}]

The best I got ’til now is:

weather.collect {|w| w[:city] if w[:weather] == :fog }.compact

But in this case, I have to call compact to remove nil values, and the example itself is not that readable as Erlang.

And even more, in the Erlang example, both city and weather are atoms. I don’t even know how to get something that makes sense and looks good like this in Ruby.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:19:20+00:00Added an answer on June 14, 2026 at 8:19 am

    First off, your data structures aren’t equivalent. The equivalent Ruby data structure to your Erlang example would be more like

    weather = [[:toronto, :rain], [:montreal, :storms], [:london, :fog], 
                [:paris, :sun], [:boston, :fog], [:vancouver, :snow]]
    

    Secondly, yes, Ruby doesn’t have list comprehensions nor pattern matching. So, the example will probably be more complex. Your list comprehension first filters all foggy cities, then projects the name. Let’s do the same in Ruby:

    weather.select {|_, weather| weather == :fog }.map(&:first)
    # => [:london, :boston]
    

    However, Ruby is centered around objects, but you are using abstract data types. With a more object-oriented data abstraction, the code would probably look more like

    weather.select(&:foggy?).map(&:city)
    

    which isn’t too bad, is it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started to learn C. I'm using Dev C++ but I don't like
I've just started to learn Drupal and decided to implement a simple Student list
I just started to learn how to use action listeners. To my understanding it
I just started to learn Rails from railstutorial.org. After installing Rails I cerated an
I just started to learn about mod rewrite process. Now, I got some problems
I have just started to learn how to code iOS apps. I have made
Backrground Information I've just started to learn stuff about drivers and the linux kernel.
Well, I've just started to learn Exceptions at college, here is a code that
I have just started learning Rails, is it required for me to learn CoffeeScript
I recently started to learn Java programming and we just reach the Java Threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.