需要用于验证ipv4和ipv6的scala功能代码 [英] Need a scala functional code for validating ipv4 and ipv6

查看:123
本文介绍了需要用于验证ipv4和ipv6的scala功能代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建用于解析IP地址的功能程序.我看到一个错误.我想要一个更简单的代码来区分ipv4和ipv6.这是JAVA代码.

import java.util.regex.Pattern;
class Solution {
  String chunkIPv4 = "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
  Pattern pattenIPv4 =
          Pattern.compile("^(" + chunkIPv4 + "\\.){3}" + chunkIPv4 + "$");

  String chunkIPv6 = "([0-9a-fA-F]{1,4})";
  Pattern pattenIPv6 =
          Pattern.compile("^(" + chunkIPv6 + "\\:){7}" + chunkIPv6 + "$");

  public String validIPAddress(String IP) {
    if (pattenIPv4.matcher(IP).matches()) return "IPv4";
    return (pattenIPv6.matcher(IP).matches()) ? "IPv6" : "Neither";
  }
} 

解决方案

假设您在注释中编写的Scala解决方案具有以下特点:

  def validIPAddress(IP: String): String = {
    if (pattenIPv4.matcher(IP).matches()) "IPv4"
    if (pattenIPv6.matcher(IP).matches()) "IPv6"
    else "Neither"
  }

第一行if行将被评估,但没有return关键字将不会返回,因此它将通过下一个条件. 您可以通过两种方法解决此问题,一种方法是添加return:

if (pattenIPv4.matcher(IP).matches()) return "IPv4"

或者最好在第二行添加else,因此您可以避免使用return,因为整个内容将作为单个表达式求值:

  def validIPAddress(IP: String): String = {
    if (pattenIPv4.matcher(IP).matches()) "IPv4"
    else if (pattenIPv6.matcher(IP).matches()) "IPv6"
    else "Neither"
  }

此外,请注意,所有这些var都可以是val,因为您没有对其进行突变,因此在scala中的一种好作法是保证它们始终具有相同的值. /p>

I was trying to construct functional program for parsing IP address. I am seeing an error. I wanted a simpler code which differentiates ipv4 to ipv6. Here is the JAVA code.

import java.util.regex.Pattern;
class Solution {
  String chunkIPv4 = "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
  Pattern pattenIPv4 =
          Pattern.compile("^(" + chunkIPv4 + "\\.){3}" + chunkIPv4 + "$");

  String chunkIPv6 = "([0-9a-fA-F]{1,4})";
  Pattern pattenIPv6 =
          Pattern.compile("^(" + chunkIPv6 + "\\:){7}" + chunkIPv6 + "$");

  public String validIPAddress(String IP) {
    if (pattenIPv4.matcher(IP).matches()) return "IPv4";
    return (pattenIPv6.matcher(IP).matches()) ? "IPv6" : "Neither";
  }
} 

解决方案

Assuming your scala solution that you wrote in the comment has the following:

  def validIPAddress(IP: String): String = {
    if (pattenIPv4.matcher(IP).matches()) "IPv4"
    if (pattenIPv6.matcher(IP).matches()) "IPv6"
    else "Neither"
  }

The first if line will be evaluated but will not return without a return keyword, so it will fall through the next conditional. You can fix that in two ways, one is to add return:

if (pattenIPv4.matcher(IP).matches()) return "IPv4"

or maybe better add an else to the second line, so you can avoid the return as the whole thing will be evaluated as a single expression:

  def validIPAddress(IP: String): String = {
    if (pattenIPv4.matcher(IP).matches()) "IPv4"
    else if (pattenIPv6.matcher(IP).matches()) "IPv6"
    else "Neither"
  }

Also, as a side note, all those vars can be vals since you are not mutating them, and it's a good practice in scala to have the guarantee that they will always have the same value.

这篇关于需要用于验证ipv4和ipv6的scala功能代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆