我有一个字符串列表,我想逐个读取字符串并将其转换为整数列表,有没有办法将每个字符转换为新的列表?
public static function sendEmail($data) {
$r_error = 1;
$r_message = "";
$r_data = array();
$q = "select * from config where type='email_detail'";
$r = self::DBrunQuery($q);
$row = self::DBfetchRow($r);
$detail = json_decode($row['value'], true);
include "phpmailer/PHPMailerAutoload.php";
if (!empty($data['email'])) {
foreach ($data as $var) {
$work_email = 'fahadansari12feb@gmail.com'; //$var['email_id'];
$name = 'fahad'; //$var['name'];
$subject = $var['subject'];
$body = $var['body'];
$cc = $var['cc_detail'];
$bcc = $var['bcc_detail'];
$file_upload = $var['upload_file'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = '5.9.144.226'; //$detail['host'];
$mail->Port = '2222'; //$detail['port'];
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'fahadansari12feb@gmail.com'; //$detail['username']; //sender email address
$mail->Password = 'mypassword'; //$detail['password']; // sender email password
$mail->setFrom('hr@excellencetechnologies.in', 'Excellence Technologies'); // name and email address from which email is send
$mail->addReplyTo('hr@excellencetechnologies.in', 'Excellence Technologies'); // reply email address with name
$mail->addAddress($work_email, $name); // name and address to whome mail is to send
if (sizeof($cc) > 0) {
foreach ($cc as $d) {
$mail->addCC($d[0], $d[1]);
}
}
if (sizeof($bcc) > 0) {
foreach ($bcc as $d2) {
$mail->addBCC($d2[0], $d2[1]);
}
}
$mail->Subject = $subject; // subject of email message
$mail->msgHTML($body); // main message
// $mail->AltBody = 'This is a plain-text message body';
//Attach an image file
if (sizeof($file_upload) > 0) {
foreach ($file_upload as $d3) {
$mail->addAttachment($d3);
}
}
//send the message, check for errors
if (!$mail->send()) {
$row3 = $mail->ErrorInfo;
} else {
$row3 = "Message sent";
}
}
}
if ($row3 != "Message sent") {
$r_error = 1;
$r_message = $row3;
$r_data['message'] = $r_message;
} else {
$r_error = 0;
$r_message = "Message Sent";
$r_data['message'] = $r_message;
}
$return = array();
$return['error'] = $r_error;
$return['data'] = $r_data;
return $return;
}
答案 0 :(得分:1)
函数式编程的一个支柱是map
,它具有以下特征:
map :: (a -> b) -> [a] -> [b]
换句话说,它需要一个函数,它需要a
类型的值并返回类型b
的值(注意这些类型可能是相同的类型,但不要be)以及a
值列表,并返回b
值列表。举个例子:
double :: Int -> Int
double = (*2)
{- equivalently written as:
double x = x * 2 -}
as = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
bs = map double as
在此示例中,地图专用于a ~ Int
和b ~ Int
,解析为
map :: (Int -> Int) -> [Int] -> [Int]
然后bs
为[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
。
为什么map
上有这么长的引物?因为它是函数式编程中许多答案的框架,包括你的问题。请注意,String
只是[Char]
的类型同义词,因此您尝试从[[Char]] -> [[Int]]
开始。哎呀,这肯定看起来像地图类型签名的最后两个术语,不是吗?让我们专注map
以这些术语运作。
map :: ([Char] -> [Int]) -> [[Char]] -> [[Int]]
但是等等,map
期待的函数看起来像是映射的结果。我们也写一下。
map :: (Char -> Int) -> [Char] -> [Int]
所以我们想要的是 double 地图,应用于某个函数f
,以便:
map (map f) :: [[Char]] -> [[Int]]
{- more idiomatically written as
(map . map) f :: [[Char]] -> [[Int]] -}
这意味着我们需要一个f :: Char -> Int
- 一些从单个字符到整数的函数。该操作的定义输入数量相当少,所以我只是写它。
digitToInt :: Char -> Int
digitToInt '0' = 0
digitToInt '1' = 1
digitToInt '2' = 2
digitToInt '3' = 3
digitToInt '4' = 4
digitToInt '5' = 5
digitToInt '6' = 6
digitToInt '7' = 7
digitToInt '8' = 8
digitToInt '9' = 9
digitToInt 'a' = 10
digitToInt 'A' = 10
digitToInt 'b' = 11
digitToInt 'B' = 11
digitToInt 'c' = 12
digitToInt 'C' = 12
digitToInt 'd' = 13
digitToInt 'D' = 13
digitToInt 'e' = 14
digitToInt 'E' = 14
digitToInt 'f' = 15
digitToInt 'F' = 15
digitToInt _ = error "Invalid digit"
但是N.B.此功能是Data.Char
import Data.Char (digitToInt)
您的结果是:
result = (map.map) digitToInt ["123","346","789"]
答案 1 :(得分:1)
import Data.List (intersperse)
f :: String :: [Int]
f stringnum = read $ "[" ++ intersperse ',' stringnum ++ "]" :: [Int]
>>> map f ["123", "456"]
[[1,2,3],[4,5,6]]