乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 36|回复: 4

[编程交流] Mapcar for (if (= ....

[复制链接]

180

主题

639

帖子

463

银币

中流砥柱

Rank: 25

铜币
897
发表于 2022-7-6 06:27:35 | 显示全部楼层 |阅读模式
I have an exercise this morning I'm working on and thought why not make use of the time to learn more about mapcar/lambda.
 
I have four variables:
 
 
If any of these variables contains the value of "Y" I want to take some action. I've been doing this in the past:
 
  1. (if (or (= front "Y") (= back "Y") (= rside "Y") (= lside "Y")) (setq a "Y"))
 
All the examples I've searched through so far show how to add 1, subtract 2, strcase, etc... How would one run this process through mapcar/lambda to achieve the same results? Or would this not be a good example of where to use mapcar in lieu of the above (if (or statement?
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-6 06:47:43 | 显示全部楼层
Based on your example for value assign to variable A
 
  1. (setq a (vl-some             '(lambda (v)                    (if (eq (eval v) "Y")                          "Y"))             '(front back rside lside)))
 
or if you want to see what variable has the value "Y"
  1. (vl-some     '(lambda (v)            (if (eq (eval v) "Y")                  v))     '(front back rside lside))
 
EDIT: Man, i saw a similar sugesstion by Ron at the swamp.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 07:07:06 | 显示全部楼层
 
 
Consider:
  1. (setq a      (car        (vl-remove-if-not          (function (lambda (x) (= "Y" x)))          (list front back rside lside)        )      ))
 
Speed test:
  1. (defun _foo1 () (vl-some   '(lambda (v)      (if (eq (eval v) "Y")        "Y"      )    )   (list front back rside lside) ))(defun _foo2 () (car   (vl-remove-if-not     (function (lambda (x) (= x "Y")))     (list front back rside lside)   ) ))
 
... Result from console:
  1. _FOO1 _FOO2 _$ (bench '(_foo1 _foo2) '() 10000)_FOO1Elapsed: 733Average: 0.0733_FOO2Elapsed: 63Average: 0.0063_$
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-6 07:17:09 | 显示全部楼层
 
I'm sold
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 07:29:28 | 显示全部楼层
 
Cheers, pBe
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-10 18:52 , Processed in 0.590761 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表