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 6962255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:11+00:00 2026-05-27T15:38:11+00:00

I have been learning to use Map more (to become more functional programmer). It

  • 0

I have been learning to use Map more (to become more functional programmer). It looks like Map wants a list as the expression to apply the function to. If the expression is not a list, then it is not happy.

I use NumberForm in this example to illustrate what I mean:

I can Map NumberForm on the whole list ok:

data = {1, 2, 3}
Map[NumberForm[#, {3, 2}] &, data]

But if I try to Map it to some specific element in the list, say the first one in the above, it does not work

data = {1, 2, 3}
Map[NumberForm[#, {3, 2}] &, data[[1]] ]

The result returned is NOT formatted. Same as original data. i.e I get back ‘1’ and not ‘1.00’ as in the other examples.

To solve, I added extra {}

data = {1, 2, 3}
Map[NumberForm[#, {3, 2}] &, {data[[1]]} ]

it works now, (just need to remove the {} from the result using First).

So I thought, then why not add this extra {} all the time and remove it in the end?
This way, I do not have to worry if what I am Map’ing function to happened to be not a list like in the above example?

So, my examples will all becomes like this:

data = {1, 2, 3}
First@Map[NumberForm[#, {3, 2}] &, { data } ]
First@Map[NumberForm[#, {3, 2}] &, { data[[1]] } ]

This way, code will works on everything and I do not have to make special check before using Map if what I happened to be applying Map to is a list or not.

Question is: Does the the above look an OK solution for the experts, or is there a better way to handle this?

  • 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-05-27T15:38:12+00:00Added an answer on May 27, 2026 at 3:38 pm

    This only happens to work because NumberForm works on lists:

    NumberForm[{1, 2, 3}, {3, 2}]
    

    gives

    {1.00, 2.00, 3.00}
    

    Map[f, {{a, b, c}}] simply maps f onto First[{{a,b,c}}], namely, onto {a,b,c}; so you get f[{a,b,c}].

    So unfortunately adding {} will not work in general.

    A simple way to do this is to define

    ClearAll[map]
    map[f_, head_[els__]] := Map[f, head[els]]
    map[f_, el_] := f[el]
    

    whence

    map[f, {a, b, c}]
    map[f, a]
    

    give

    {f[a], f[b], f[c]}
    f[a]
    

    However this does not allow for the Map[f,expr,levelspec] form (which however is easy enough to implement).

    This also works in this case:

    map[f, g[a, b, c]] == Map[f, g[a, b, c]]
    (*
    True
    *)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning to use Emacs for a little while now. So far
I have been learning more and more javascript; it's a necessity at my job.
I have recently been learning more about design patterns and thought I'd take a
I have been learning some of the more detailed loopholes of JS here and
I'm learning Python and have been trying to understand more about the details of
I have been learning to use prepared and bound statements for my sql queries,
I have been learning C# for the past few days for use with ASP.NET
I have been learning to use hibernate for a couple of months. I am
I'm not a very experience programmer and have been learning HTML/CSS/JS on the fly.
I have been spending time learning how to use the iPhone SDK. I've read

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.